Skip to content

Commit dad33b0

Browse files
teknoraverNobody
authored andcommitted
bpf: make unprivileged BPF a compile time choice
Add a compile time option to permanently disable unprivileged BPF and the corresponding sysctl handler so that there's absolutely no concern about unprivileged BPF being enabled from userspace during runtime. Special purpose kernels can benefit from the build-time assurance that unprivileged eBPF is disabled in all of their kernel builds rather than having to rely on userspace to permanently disable it at boot time. The default behaviour is left unchanged, which is: unprivileged BPF compiled in but disabled at boot. Signed-off-by: Matteo Croce <[email protected]>
1 parent 88a7dba commit dad33b0

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

kernel/bpf/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ config BPF_JIT_DEFAULT_ON
6767
def_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON
6868
depends on HAVE_EBPF_JIT && BPF_JIT
6969

70+
config BPF_UNPRIV
71+
bool "Unprivileged BPF"
72+
default y
73+
depends on BPF_SYSCALL
74+
help
75+
Enables unprivileged BPF and the corresponding
76+
/proc/sys/kernel/unprivileged_bpf_disabled knob.
77+
7078
config BPF_UNPRIV_DEFAULT_OFF
7179
bool "Disable unprivileged BPF by default"
7280
default y
73-
depends on BPF_SYSCALL
81+
depends on BPF_UNPRIV
7482
help
7583
Disables unprivileged BPF by default by setting the corresponding
7684
/proc/sys/kernel/unprivileged_bpf_disabled knob to 2. An admin can

kernel/bpf/syscall.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ static DEFINE_IDR(link_idr);
5353
static DEFINE_SPINLOCK(link_idr_lock);
5454

5555
int sysctl_unprivileged_bpf_disabled __read_mostly =
56-
IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
56+
IS_BUILTIN(CONFIG_BPF_UNPRIV) ?
57+
(IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0)
58+
: 1;
5759

5860
static const struct bpf_map_ops * const bpf_map_types[] = {
5961
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)

kernel/sysctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ void __weak unpriv_ebpf_notify(int new_state)
184184
{
185185
}
186186

187+
#ifdef CONFIG_BPF_UNPRIV
187188
static int bpf_unpriv_handler(struct ctl_table *table, int write,
188189
void *buffer, size_t *lenp, loff_t *ppos)
189190
{
@@ -206,6 +207,7 @@ static int bpf_unpriv_handler(struct ctl_table *table, int write,
206207

207208
return ret;
208209
}
210+
#endif /* CONFIG_BPF_UNPRIV */
209211
#endif /* CONFIG_BPF_SYSCALL && CONFIG_SYSCTL */
210212

211213
/*
@@ -2300,6 +2302,7 @@ static struct ctl_table kern_table[] = {
23002302
},
23012303
#endif
23022304
#ifdef CONFIG_BPF_SYSCALL
2305+
#ifdef CONFIG_BPF_UNPRIV
23032306
{
23042307
.procname = "unprivileged_bpf_disabled",
23052308
.data = &sysctl_unprivileged_bpf_disabled,
@@ -2309,6 +2312,7 @@ static struct ctl_table kern_table[] = {
23092312
.extra1 = SYSCTL_ZERO,
23102313
.extra2 = SYSCTL_TWO,
23112314
},
2315+
#endif
23122316
{
23132317
.procname = "bpf_stats_enabled",
23142318
.data = &bpf_stats_enabled_key.key,

0 commit comments

Comments
 (0)