Skip to content

Commit fdadd04

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: fix bpf_jit_limit knob for PAGE_SIZE >= 64K
Michael and Sandipan report: Commit ede95a6 introduced a bpf_jit_limit tuneable to limit BPF JIT allocations. At compile time it defaults to PAGE_SIZE * 40000, and is adjusted again at init time if MODULES_VADDR is defined. For ppc64 kernels, MODULES_VADDR isn't defined, so we're stuck with the compile-time default at boot-time, which is 0x9c400000 when using 64K page size. This overflows the signed 32-bit bpf_jit_limit value: root@ubuntu:/tmp# cat /proc/sys/net/core/bpf_jit_limit -1673527296 and can cause various unexpected failures throughout the network stack. In one case `strace dhclient eth0` reported: setsockopt(5, SOL_SOCKET, SO_ATTACH_FILTER, {len=11, filter=0x105dd27f8}, 16) = -1 ENOTSUPP (Unknown error 524) and similar failures can be seen with tools like tcpdump. This doesn't always reproduce however, and I'm not sure why. The more consistent failure I've seen is an Ubuntu 18.04 KVM guest booted on a POWER9 host would time out on systemd/netplan configuring a virtio-net NIC with no noticeable errors in the logs. Given this and also given that in near future some architectures like arm64 will have a custom area for BPF JIT image allocations we should get rid of the BPF_JIT_LIMIT_DEFAULT fallback / default entirely. For 4.21, we have an overridable bpf_jit_alloc_exec(), bpf_jit_free_exec() so therefore add another overridable bpf_jit_alloc_exec_limit() helper function which returns the possible size of the memory area for deriving the default heuristic in bpf_jit_charge_init(). Like bpf_jit_alloc_exec() and bpf_jit_free_exec(), the new bpf_jit_alloc_exec_limit() assumes that module_alloc() is the default JIT memory provider, and therefore in case archs implement their custom module_alloc() we use MODULES_{END,_VADDR} for limits and otherwise for vmalloc_exec() cases like on ppc64 we use VMALLOC_{END,_START}. Additionally, for archs supporting large page sizes, we should change the sysctl to be handled as long to not run into sysctl restrictions in future. Fixes: ede95a6 ("bpf: add bpf_jit_limit knob to restrict unpriv allocations") Reported-by: Sandipan Das <sandipan@linux.ibm.com> Reported-by: Michael Roth <mdroth@linux.vnet.ibm.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Tested-by: Michael Roth <mdroth@linux.vnet.ibm.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent aca1a80 commit fdadd04

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
861861
extern int bpf_jit_enable;
862862
extern int bpf_jit_harden;
863863
extern int bpf_jit_kallsyms;
864-
extern int bpf_jit_limit;
864+
extern long bpf_jit_limit;
865865

866866
typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
867867

‎kernel/bpf/core.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,11 @@ void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
365365
}
366366

367367
#ifdef CONFIG_BPF_JIT
368-
# define BPF_JIT_LIMIT_DEFAULT (PAGE_SIZE * 40000)
369-
370368
/* All BPF JIT sysctl knobs here. */
371369
int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
372370
int bpf_jit_harden __read_mostly;
373371
int bpf_jit_kallsyms __read_mostly;
374-
int bpf_jit_limit __read_mostly = BPF_JIT_LIMIT_DEFAULT;
372+
long bpf_jit_limit __read_mostly;
375373

376374
static __always_inline void
377375
bpf_get_prog_addr_region(const struct bpf_prog *prog,
@@ -580,16 +578,27 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
580578

581579
static atomic_long_t bpf_jit_current;
582580

581+
/* Can be overridden by an arch's JIT compiler if it has a custom,
582+
* dedicated BPF backend memory area, or if neither of the two
583+
* below apply.
584+
*/
585+
u64 __weak bpf_jit_alloc_exec_limit(void)
586+
{
583587
#if defined(MODULES_VADDR)
588+
return MODULES_END - MODULES_VADDR;
589+
#else
590+
return VMALLOC_END - VMALLOC_START;
591+
#endif
592+
}
593+
584594
static int __init bpf_jit_charge_init(void)
585595
{
586596
/* Only used as heuristic here to derive limit. */
587-
bpf_jit_limit = min_t(u64, round_up((MODULES_END - MODULES_VADDR) >> 2,
588-
PAGE_SIZE), INT_MAX);
597+
bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
598+
PAGE_SIZE), LONG_MAX);
589599
return 0;
590600
}
591601
pure_initcall(bpf_jit_charge_init);
592-
#endif
593602

594603
static int bpf_jit_charge_modmem(u32 pages)
595604
{

‎net/core/sysctl_net_core.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static int two __maybe_unused = 2;
2828
static int min_sndbuf = SOCK_MIN_SNDBUF;
2929
static int min_rcvbuf = SOCK_MIN_RCVBUF;
3030
static int max_skb_frags = MAX_SKB_FRAGS;
31+
static long long_one __maybe_unused = 1;
32+
static long long_max __maybe_unused = LONG_MAX;
3133

3234
static int net_msg_warn; /* Unused, but still a sysctl */
3335

@@ -289,6 +291,17 @@ proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
289291

290292
return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
291293
}
294+
295+
static int
296+
proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
297+
void __user *buffer, size_t *lenp,
298+
loff_t *ppos)
299+
{
300+
if (!capable(CAP_SYS_ADMIN))
301+
return -EPERM;
302+
303+
return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
304+
}
292305
#endif
293306

294307
static struct ctl_table net_core_table[] = {
@@ -398,10 +411,11 @@ static struct ctl_table net_core_table[] = {
398411
{
399412
.procname = "bpf_jit_limit",
400413
.data = &bpf_jit_limit,
401-
.maxlen = sizeof(int),
414+
.maxlen = sizeof(long),
402415
.mode = 0600,
403-
.proc_handler = proc_dointvec_minmax_bpf_restricted,
404-
.extra1 = &one,
416+
.proc_handler = proc_dolongvec_minmax_bpf_restricted,
417+
.extra1 = &long_one,
418+
.extra2 = &long_max,
405419
},
406420
#endif
407421
{

0 commit comments

Comments
 (0)