Skip to content

Commit 6fde36d

Browse files
Alexei Starovoitovgregkh
authored andcommitted
bpf: introduce BPF_JIT_ALWAYS_ON config
[ upstream commit 290af86 ] The BPF interpreter has been used as part of the spectre 2 attack CVE-2017-5715. A quote from goolge project zero blog: "At this point, it would normally be necessary to locate gadgets in the host kernel code that can be used to actually leak data by reading from an attacker-controlled location, shifting and masking the result appropriately and then using the result of that as offset to an attacker-controlled address for a load. But piecing gadgets together and figuring out which ones work in a speculation context seems annoying. So instead, we decided to use the eBPF interpreter, which is built into the host kernel - while there is no legitimate way to invoke it from inside a VM, the presence of the code in the host kernel's text section is sufficient to make it usable for the attack, just like with ordinary ROP gadgets." To make attacker job harder introduce BPF_JIT_ALWAYS_ON config option that removes interpreter from the kernel in favor of JIT-only mode. So far eBPF JIT is supported by: x64, arm64, arm32, sparc64, s390, powerpc64, mips64 The start of JITed program is randomized and code page is marked as read-only. In addition "constant blinding" can be turned on with net.core.bpf_jit_harden v2->v3: - move __bpf_prog_ret0 under ifdef (Daniel) v1->v2: - fix init order, test_bpf and cBPF (Daniel's feedback) - fix offloaded bpf (Jakub's feedback) - add 'return 0' dummy in case something can invoke prog->bpf_func - retarget bpf tree. For bpf-next the patch would need one extra hunk. It will be sent when the trees are merged back to net-next Considered doing: int bpf_jit_enable __read_mostly = BPF_EBPF_JIT_DEFAULT; but it seems better to land the patch as-is and in bpf-next remove bpf_jit_enable global variable from all JITs, consolidate in one place and remove this jit_init() function. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fdd88d7 commit 6fde36d

6 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎init/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,13 @@ config BPF_SYSCALL
13421342
Enable the bpf() system call that allows to manipulate eBPF
13431343
programs and maps via file descriptors.
13441344

1345+
config BPF_JIT_ALWAYS_ON
1346+
bool "Permanently enable BPF JIT and remove BPF interpreter"
1347+
depends on BPF_SYSCALL && HAVE_EBPF_JIT && BPF_JIT
1348+
help
1349+
Enables BPF JIT and removes BPF interpreter to avoid
1350+
speculative execution of BPF instructions by the interpreter
1351+
13451352
config SHMEM
13461353
bool "Use full shmem filesystem" if EXPERT
13471354
default y

‎kernel/bpf/core.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
760760
}
761761
EXPORT_SYMBOL_GPL(__bpf_call_base);
762762

763+
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
763764
/**
764765
* __bpf_prog_run - run eBPF program on a given context
765766
* @ctx: is the data we are operating on
@@ -1310,6 +1311,14 @@ EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
13101311
EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
13111312
};
13121313

1314+
#else
1315+
static unsigned int __bpf_prog_ret0(const void *ctx,
1316+
const struct bpf_insn *insn)
1317+
{
1318+
return 0;
1319+
}
1320+
#endif
1321+
13131322
bool bpf_prog_array_compatible(struct bpf_array *array,
13141323
const struct bpf_prog *fp)
13151324
{
@@ -1357,9 +1366,13 @@ static int bpf_check_tail_call(const struct bpf_prog *fp)
13571366
*/
13581367
struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
13591368
{
1369+
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
13601370
u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
13611371

13621372
fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1373+
#else
1374+
fp->bpf_func = __bpf_prog_ret0;
1375+
#endif
13631376

13641377
/* eBPF JITs can rewrite the program in case constant
13651378
* blinding is active. However, in case of error during
@@ -1368,6 +1381,12 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
13681381
* be JITed, but falls back to the interpreter.
13691382
*/
13701383
fp = bpf_int_jit_compile(fp);
1384+
#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1385+
if (!fp->jited) {
1386+
*err = -ENOTSUPP;
1387+
return fp;
1388+
}
1389+
#endif
13711390
bpf_prog_lock_ro(fp);
13721391

13731392
/* The tail call compatibility check can only be done at

‎lib/test_bpf.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6207,9 +6207,8 @@ static struct bpf_prog *generate_filter(int which, int *err)
62076207
return NULL;
62086208
}
62096209
}
6210-
/* We don't expect to fail. */
62116210
if (*err) {
6212-
pr_cont("FAIL to attach err=%d len=%d\n",
6211+
pr_cont("FAIL to prog_create err=%d len=%d\n",
62136212
*err, fprog.len);
62146213
return NULL;
62156214
}
@@ -6233,6 +6232,10 @@ static struct bpf_prog *generate_filter(int which, int *err)
62336232
* checks.
62346233
*/
62356234
fp = bpf_prog_select_runtime(fp, err);
6235+
if (*err) {
6236+
pr_cont("FAIL to select_runtime err=%d\n", *err);
6237+
return NULL;
6238+
}
62366239
break;
62376240
}
62386241

@@ -6418,8 +6421,8 @@ static __init int test_bpf(void)
64186421
pass_cnt++;
64196422
continue;
64206423
}
6421-
6422-
return err;
6424+
err_cnt++;
6425+
continue;
64236426
}
64246427

64256428
pr_cont("jited:%u ", fp->jited);

‎net/core/filter.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,11 +1053,9 @@ static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
10531053
*/
10541054
goto out_err_free;
10551055

1056-
/* We are guaranteed to never error here with cBPF to eBPF
1057-
* transitions, since there's no issue with type compatibility
1058-
* checks on program arrays.
1059-
*/
10601056
fp = bpf_prog_select_runtime(fp, &err);
1057+
if (err)
1058+
goto out_err_free;
10611059

10621060
kfree(old_prog);
10631061
return fp;

‎net/core/sysctl_net_core.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ static struct ctl_table net_core_table[] = {
325325
.data = &bpf_jit_enable,
326326
.maxlen = sizeof(int),
327327
.mode = 0644,
328+
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
328329
.proc_handler = proc_dointvec
330+
#else
331+
.proc_handler = proc_dointvec_minmax,
332+
.extra1 = &one,
333+
.extra2 = &one,
334+
#endif
329335
},
330336
# ifdef CONFIG_HAVE_EBPF_JIT
331337
{

‎net/socket.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,15 @@ static int __init sock_init(void)
26422642

26432643
core_initcall(sock_init); /* early initcall */
26442644

2645+
static int __init jit_init(void)
2646+
{
2647+
#ifdef CONFIG_BPF_JIT_ALWAYS_ON
2648+
bpf_jit_enable = 1;
2649+
#endif
2650+
return 0;
2651+
}
2652+
pure_initcall(jit_init);
2653+
26452654
#ifdef CONFIG_PROC_FS
26462655
void socket_seq_show(struct seq_file *seq)
26472656
{

0 commit comments

Comments
 (0)