Skip to contentSkip to conversation

bpf, seccomp: fix false positive preemption splat for cbpf->ebpf progs

PATCH Superseded Daniel Borkmann 2 participants 4 messages Feb 20, 2019

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 bpf, seccomp: fix false positive preemption splat for cbpf->ebpf progs

In 568f196756ad ("bpf: check that BPF programs run with preemption disabled") a check was added for BPF_PROG_RUN() that for every invocation preemption has to be disabled to not break eBPF assumptions (e.g. per-cpu map). Of course this does not count for seccomp because only cBPF -> eBPF is loaded here and it does not make use of any functionality that would require this assertion. Fix this false positive by adding and using __BPF_PROG_RUN() variant that does not have the cant_sleep(); check.

Fixes 568f196756ad ("bpf: check that BPF programs run with preemption disabled")
Reported-by [email protected]
---
 include/linux/filter.h | 9 ++++++++-
 kernel/seccomp.c       | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/filter.h +8 -1
index f32b3ec..2648fd7 100644
@@ -533,7 +533,14 @@ struct sk_filter {
 	struct bpf_prog	*prog;
 };
 
-#define BPF_PROG_RUN(filter, ctx)  ({ cant_sleep(); (*(filter)->bpf_func)(ctx, (filter)->insnsi); })
+#define bpf_prog_run__non_preempt(prog, ctx)	\
+	({ cant_sleep(); __BPF_PROG_RUN(prog, ctx); })
+/* Native eBPF or cBPF -> eBPF transitions. Preemption must be disabled. */
+#define BPF_PROG_RUN(prog, ctx)			\
+	bpf_prog_run__non_preempt(prog, ctx)
+/* Direct use for cBPF -> eBPF only, but not for native eBPF. */
+#define __BPF_PROG_RUN(prog, ctx)		\
+	(*(prog)->bpf_func)(ctx, (prog)->insnsi)
 
 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
 
kernel/seccomp.c +1 -1
index e815781..826d4e4 100644
@@ -268,7 +268,7 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd,
 	 * value always takes priority (ignoring the DATA).
 	 */
 	for (; f; f = f->prev) {
-		u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
+		u32 cur_ret = __BPF_PROG_RUN(f->prog, sd);
 
 		if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
 			ret = cur_ret;
Signature · 1 line
2.9.5
#1 lore raw
Quoted 21 lines · Daniel Borkmann

In 568f196756ad ("bpf: check that BPF programs run with preemption disabled") a check was added for BPF_PROG_RUN() that for every invocation preemption has to be disabled to not break eBPF assumptions (e.g. per-cpu map). Of course this does not count for seccomp because only cBPF -> eBPF is loaded here and it does not make use of any functionality that would require this assertion. Fix this false positive by adding and using __BPF_PROG_RUN() variant that does not have the cant_sleep(); check.

Fixes 568f196756ad ("bpf: check that BPF programs run with preemption disabled")
Reported-by [email protected]
---
 include/linux/filter.h | 9 ++++++++-
 kernel/seccomp.c       | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/filter.h +0 -0
index f32b3ec..2648fd7 100644

include/linux/filter.h struct sk_filter { · line 541

Quoted 10 lines
include/linux/filter.h +6 -1
@@ -533,7 +533,14 @@ struct sk_filter {
 	struct bpf_prog	*prog;
 };
 
-#define BPF_PROG_RUN(filter, ctx)  ({ cant_sleep(); (*(filter)->bpf_func)(ctx, (filter)->insnsi); })
+#define bpf_prog_run__non_preempt(prog, ctx)	\
+	({ cant_sleep(); __BPF_PROG_RUN(prog, ctx); })
+/* Native eBPF or cBPF -> eBPF transitions. Preemption must be disabled. */
+#define BPF_PROG_RUN(prog, ctx)			\
+	bpf_prog_run__non_preempt(prog, ctx)
+/* Direct use for cBPF -> eBPF only, but not for native eBPF. */

I think the comment is too abstract.
May be it should say that this is seccomp cBPF only ? And macro name should be explicit as well ?

Quoted 21 lines
include/linux/filter.h +2 -0
+#define __BPF_PROG_RUN(prog, ctx)		\
+	(*(prog)->bpf_func)(ctx, (prog)->insnsi)
 
 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
 
kernel/seccomp.c +1 -1
index e815781..826d4e4 100644
@@ -268,7 +268,7 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd,
 	 * value always takes priority (ignoring the DATA).
 	 */
 	for (; f; f = f->prev) {
-		u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
+		u32 cur_ret = __BPF_PROG_RUN(f->prog, sd);
 
 		if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
 			ret = cur_ret;
Signature · 1 line
2.9.5
author maintainer
#2 lore raw
Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Daniel Borkmann

In 568f196756ad ("bpf: check that BPF programs run with preemption disabled") a check was added for BPF_PROG_RUN() that for every invocation preemption has to be disabled to not break eBPF assumptions (e.g. per-cpu map). Of course this does not count for seccomp because only cBPF -> eBPF is loaded here and it does not make use of any functionality that would require this assertion. Fix this false positive by adding and using __BPF_PROG_RUN() variant that does not have the cant_sleep(); check.

Fixes 568f196756ad ("bpf: check that BPF programs run with preemption disabled")
Reported-by [email protected]
---
 include/linux/filter.h | 9 ++++++++-
 kernel/seccomp.c       | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/filter.h +0 -0
index f32b3ec..2648fd7 100644
@@ -533,7 +533,14 @@ struct sk_filter {
 	struct bpf_prog	*prog;
 };
 
Quoted 11 lines

include/linux/filter.h struct sk_filter { · line 541

Quoted 7 lines
include/linux/filter.h +6 -1
-#define BPF_PROG_RUN(filter, ctx)  ({ cant_sleep(); (*(filter)->bpf_func)(ctx, (filter)->insnsi); })
+#define bpf_prog_run__non_preempt(prog, ctx)	\
+	({ cant_sleep(); __BPF_PROG_RUN(prog, ctx); })
+/* Native eBPF or cBPF -> eBPF transitions. Preemption must be disabled. */
+#define BPF_PROG_RUN(prog, ctx)			\
+	bpf_prog_run__non_preempt(prog, ctx)
+/* Direct use for cBPF -> eBPF only, but not for native eBPF. */

I think the comment is too abstract.
May be it should say that this is seccomp cBPF only ? And macro name should be explicit as well ?

I think macro naming is probably okay imho as used internally as well from BPF_PROG_RUN(), but I'll improve the comment to state seccomp specifically as an example there and providing some more background.

Quoted 21 lines
Quoted 21 lines
include/linux/filter.h +2 -0
+#define __BPF_PROG_RUN(prog, ctx)		\
+	(*(prog)->bpf_func)(ctx, (prog)->insnsi)
 
 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
 
kernel/seccomp.c +1 -1
index e815781..826d4e4 100644
@@ -268,7 +268,7 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd,
 	 * value always takes priority (ignoring the DATA).
 	 */
 	for (; f; f = f->prev) {
-		u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
+		u32 cur_ret = __BPF_PROG_RUN(f->prog, sd);
 
 		if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
 			ret = cur_ret;
Signature · 1 line
2.9.5
#3 lore raw
Quoted 39 lines · Daniel Borkmann
Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Daniel Borkmann

In 568f196756ad ("bpf: check that BPF programs run with preemption disabled") a check was added for BPF_PROG_RUN() that for every invocation preemption has to be disabled to not break eBPF assumptions (e.g. per-cpu map). Of course this does not count for seccomp because only cBPF -> eBPF is loaded here and it does not make use of any functionality that would require this assertion. Fix this false positive by adding and using __BPF_PROG_RUN() variant that does not have the cant_sleep(); check.

Fixes 568f196756ad ("bpf: check that BPF programs run with preemption disabled")
Reported-by [email protected]
---
 include/linux/filter.h | 9 ++++++++-
 kernel/seccomp.c       | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/filter.h +0 -0
index f32b3ec..2648fd7 100644
@@ -533,7 +533,14 @@ struct sk_filter {
     struct bpf_prog *prog;
 };
 
Quoted 11 lines

include/linux/filter.h struct sk_filter { · line 541

Quoted 7 lines
include/linux/filter.h +6 -1
-#define BPF_PROG_RUN(filter, ctx)  ({ cant_sleep(); (*(filter)->bpf_func)(ctx, (filter)->insnsi); })
+#define bpf_prog_run__non_preempt(prog, ctx)        \
+    ({ cant_sleep(); __BPF_PROG_RUN(prog, ctx); })
+/* Native eBPF or cBPF -> eBPF transitions. Preemption must be disabled. */
+#define BPF_PROG_RUN(prog, ctx)                     \
+    bpf_prog_run__non_preempt(prog, ctx)
+/* Direct use for cBPF -> eBPF only, but not for native eBPF. */

I think the comment is too abstract.
May be it should say that this is seccomp cBPF only ? And macro name should be explicit as well ?

Quoted 4 lines

I think macro naming is probably okay imho as used internally as well from BPF_PROG_RUN(), but I'll improve the comment to state seccomp specifically as an example there and providing some more background.

I'm worried about misuse of the macro.
If there was a word seccomp in it it would made people think much harder before calling it.