Skip to contentSkip to conversation

bpf: Fix and simplify bpf_task_work_ctx lifetime management

PATCH In review Mykyta Yatsenko 3 participants 8 messages Feb 11 BPF

lore
Share & tools
lore
#0 lore raw

cover bpf: Fix and simplify bpf_task_work_ctx lifetime management

From: Mykyta Yatsenko <[email protected]>

bpf_task_work_ctx can be accessed concurrently by both sleepable BPF programs (which hold rcu_tasks_trace lock) and non-sleepable BPF programs (which hold RCU lock). The current code frees bpf_task_work_ctx immediately upon the last refcount_dec, without waiting for either RCU grace period. This may lead to use after free, as there is a short race window in bpf_task_work_acquire_ctx() between calls to bpf_task_work_fetch_ctx() and bpf_task_work_ctx_tryget().

Patch 1 fixes the freeing path by deferring the release through call_rcu_tasks_trace followed by call_rcu, ensuring both grace periods have elapsed before the memory is released (similar to bpf_async_cb). When called from IRQ-disabled context (e.g. NMI from tracing BPF programs), irq_work is used to defer call_rcu_tasks_trace to a safe context, since call_rcu_tasks_trace cannot be called with IRQs disabled.

Patch 2 replaces bpf_mem_alloc/bpf_mem_free with the new kmalloc_nolock/kfree_nolock API for bpf_task_work_ctx allocation. Now that kmalloc can be used from NMI context, it is a simpler and more standard mechanism.

Mykyta Yatsenko (2):
bpf: Fix use-after-free in bpf_task_work_ctx freeing
bpf: Use kmalloc_nolock for bpf_task_work_ctx allocation

 kernel/bpf/helpers.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 bpf: Fix use-after-free in bpf_task_work_ctx freeing

From: Mykyta Yatsenko <[email protected]>

Current code frees the context immediately when the refcount drops to zero, without waiting for any RCU grace period. This is unsafe: a concurrent BPF program calling bpf_task_work_schedule() may still hold the ctx without a reference for a very short period of time in bpf_task_work_acquire_ctx().

Fix this by deferring the free through two RCU grace periods:

    call_rcu_tasks_trace -> call_rcu -> free

This is similar to bpf_async_cb freeing.

Since bpf_task_work_ctx_put() can be called from IRQ-disabled context (e.g. NMI from tracing BPF programs), and call_rcu_tasks_trace is not safe there, use irq_work to defer the initial call_rcu_tasks_trace to a safe context.

Fixes 38aa7003e369 ("bpf: task work scheduling kfuncs")
---
 kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
kernel/bpf/helpers.c +31 -6
index 7ac32798eb04..414e5b55cd37 100644
@@ -4160,6 +4160,31 @@ static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)
 	}
 }
 
+static void bpf_task_work_ctx_free_rcu(struct rcu_head *rcu)
+{
+	struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+	bpf_task_work_ctx_reset(ctx);
+	/* bpf_mem_free expects migration to be disabled */
+	migrate_disable();
+	bpf_mem_free(&bpf_global_ma, ctx);
+	migrate_enable();
+}
+
+static void bpf_task_work_ctx_free_rcu_tasks_trace(struct rcu_head *rcu)
+{
+	struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+	call_rcu(&ctx->rcu, bpf_task_work_ctx_free_rcu);
+}
+
+static void bpf_task_work_ctx_delete(struct irq_work *irq_work)
+{
+	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+
+	call_rcu_tasks_trace(&ctx->rcu, bpf_task_work_ctx_free_rcu_tasks_trace);
+}
+
 static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)
 {
 	return refcount_inc_not_zero(&ctx->refcnt);
@@ -4170,12 +4195,12 @@ static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)
 	if (!refcount_dec_and_test(&ctx->refcnt))
 		return;
 
-	bpf_task_work_ctx_reset(ctx);
-
-	/* bpf_mem_free expects migration to be disabled */
-	migrate_disable();
-	bpf_mem_free(&bpf_global_ma, ctx);
-	migrate_enable();
+	if (irqs_disabled()) {
+		init_irq_work(&ctx->irq_work, bpf_task_work_ctx_delete);
+		irq_work_queue(&ctx->irq_work);
+	} else {
+		bpf_task_work_ctx_delete(&ctx->irq_work);
+	}
 }
 
 static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Fix use-after-free in bpf_task_work_ctx freeing

Quoted 45 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Current code frees the context immediately when the refcount drops to zero, without waiting for any RCU grace period. This is unsafe: a concurrent BPF program calling bpf_task_work_schedule() may still hold the ctx without a reference for a very short period of time in bpf_task_work_acquire_ctx().

Fix this by deferring the free through two RCU grace periods:

    call_rcu_tasks_trace -> call_rcu -> free

This is similar to bpf_async_cb freeing.

Since bpf_task_work_ctx_put() can be called from IRQ-disabled context (e.g. NMI from tracing BPF programs), and call_rcu_tasks_trace is not safe there, use irq_work to defer the initial call_rcu_tasks_trace to a safe context.

Fixes 38aa7003e369 ("bpf: task work scheduling kfuncs")
---
 kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
kernel/bpf/helpers.c +13 -0
index 7ac32798eb04..414e5b55cd37 100644
@@ -4160,6 +4160,31 @@ static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)
        }
 }
 
+static void bpf_task_work_ctx_free_rcu(struct rcu_head *rcu)
+{
+       struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+       bpf_task_work_ctx_reset(ctx);
+       /* bpf_mem_free expects migration to be disabled */
+       migrate_disable();
+       bpf_mem_free(&bpf_global_ma, ctx);
+       migrate_enable();
+}
+
+static void bpf_task_work_ctx_free_rcu_tasks_trace(struct rcu_head *rcu)
+{

kernel/bpf/helpers.c static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx) · line 4185

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+       call_rcu(&ctx->rcu, bpf_task_work_ctx_free_rcu);
+}
+
+static void bpf_task_work_ctx_delete(struct irq_work *irq_work)
+{
+       struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+
+       call_rcu_tasks_trace(&ctx->rcu, bpf_task_work_ctx_free_rcu_tasks_trace);

with timers we already hard-coded assumption/guarantee that RCU tasks trace GP implies RCU GP (Paul is going to or already did codify this guarantee going forward), let's drop this extra hop here as well

kernel/bpf/helpers.c static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx) · line 4173

Quoted 10 lines
kernel/bpf/helpers.c +2 -1
+}
+
 static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)
 {
        return refcount_inc_not_zero(&ctx->refcnt);
@@ -4170,12 +4195,12 @@ static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)
        if (!refcount_dec_and_test(&ctx->refcnt))
                return;
 
-       bpf_task_work_ctx_reset(ctx);

it should still be correct to put prog and task here, even from NMI, so let's do this here early?

Quoted 17 lines
kernel/bpf/helpers.c +6 -6
-
-       /* bpf_mem_free expects migration to be disabled */
-       migrate_disable();
-       bpf_mem_free(&bpf_global_ma, ctx);
-       migrate_enable();
+       if (irqs_disabled()) {
+               init_irq_work(&ctx->irq_work, bpf_task_work_ctx_delete);
+               irq_work_queue(&ctx->irq_work);
+       } else {
+               bpf_task_work_ctx_delete(&ctx->irq_work);
+       }
 }
 
 static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
--

2.47.3

#3 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Fix use-after-free in bpf_task_work_ctx freeing

In reply to Mykyta Yatsenko
Quoted 5 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Current code frees the context immediately when the refcount drops to zero, without waiting for any RCU grace period. This is unsafe:

This statement is misleading. It's correct and incorrect at the same time. bpf_mem_free() adds to linked list for immediate reuse
via bpf_mem_alloc(), but it doesn't actually free it to the slab for the rest of the kernel to use until both rcu_task_trace and rcu GPs.

So I'm not yet sure what immediate reuse of bpf_task_work_ctx can create problems. Probably,
but the commit log should describe the exact scenario.

Quoted 26 lines

concurrent BPF program calling bpf_task_work_schedule() may still hold the ctx without a reference for a very short period of time in bpf_task_work_acquire_ctx().

Fix this by deferring the free through two RCU grace periods:

    call_rcu_tasks_trace -> call_rcu -> free

This is similar to bpf_async_cb freeing.

Since bpf_task_work_ctx_put() can be called from IRQ-disabled context (e.g. NMI from tracing BPF programs), and call_rcu_tasks_trace is not safe there, use irq_work to defer the initial call_rcu_tasks_trace to a safe context.

Fixes 38aa7003e369 ("bpf: task work scheduling kfuncs")
---
 kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
kernel/bpf/helpers.c +0 -0
index 7ac32798eb04..414e5b55cd37 100644
@@ -4160,6 +4160,31 @@ static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)
        }
 }

kernel/bpf/helpers.c static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx) · line 4171

Quoted 10 lines
kernel/bpf/helpers.c +9 -0
 
+static void bpf_task_work_ctx_free_rcu(struct rcu_head *rcu)
+{
+       struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+       bpf_task_work_ctx_reset(ctx);
+       /* bpf_mem_free expects migration to be disabled */
+       migrate_disable();
+       bpf_mem_free(&bpf_global_ma, ctx);
+       migrate_enable();

rcu machinery disables bh before calling callbacks which I think prevents migration too.
Pls check with Paul.

#4 lore raw

PATCH 2/2 bpf: Use kmalloc_nolock for bpf_task_work_ctx allocation

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Replace bpf_mem_alloc/bpf_mem_free with kmalloc_nolock/kfree_nolock for allocating and freeing bpf_task_work_ctx. Now that kmalloc can be used from NMI context, it is the right generic mechanism for memory allocation in BPF kfuncs, even in restricted contexts like tracing programs.

---
 kernel/bpf/helpers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/bpf/helpers.c +3 -6
index 414e5b55cd37..d25aa51c80d3 100644
@@ -4165,10 +4165,7 @@ static void bpf_task_work_ctx_free_rcu(struct rcu_head *rcu)
 	struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
 
 	bpf_task_work_ctx_reset(ctx);
-	/* bpf_mem_free expects migration to be disabled */
-	migrate_disable();
-	bpf_mem_free(&bpf_global_ma, ctx);
-	migrate_enable();
+	kfree_nolock(ctx);
 }
 
 static void bpf_task_work_ctx_free_rcu_tasks_trace(struct rcu_head *rcu)
@@ -4295,7 +4292,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
 	if (ctx)
 		return ctx;
 
-	ctx = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_task_work_ctx));
+	ctx = kmalloc_nolock(sizeof(struct bpf_task_work_ctx), 0, map->numa_node);
 	if (!ctx)
 		return ERR_PTR(-ENOMEM);
 
@@ -4309,7 +4306,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
 		 * tw->ctx is set by concurrent BPF program, release allocated
 		 * memory and try to reuse already set context.
 		 */
-		bpf_mem_free(&bpf_global_ma, ctx);
+		kfree_nolock(ctx);
 		return old_ctx;
 	}
 
Signature · 1 line
2.47.3
Acked-by on v1 2/2
#5 lore raw

Re: [PATCH bpf-next v1 2/2] bpf: Use kmalloc_nolock for bpf_task_work_ctx allocation

Quoted 8 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Replace bpf_mem_alloc/bpf_mem_free with kmalloc_nolock/kfree_nolock for allocating and freeing bpf_task_work_ctx. Now that kmalloc can be used from NMI context, it is the right generic mechanism for memory allocation in BPF kfuncs, even in restricted contexts like tracing programs.

Quoted 5 lines
---
 kernel/bpf/helpers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

LGTM

BTW, file dynptr is also still using bpf_mem_free, consider changing that to kmallock_nolock as a follow up as well, please (and I guess in general we should do an audit pass on what are the other bpf_mem_alloc places that should be converted, but Alexei is a more authoritative on what are the remaining constraints that would prevent some functionality to be switched to kmallock_nolock).

Quoted 37 lines
kernel/bpf/helpers.c +3 -7
index 414e5b55cd37..d25aa51c80d3 100644
@@ -4165,10 +4165,7 @@ static void bpf_task_work_ctx_free_rcu(struct rcu_head *rcu)
        struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
 
        bpf_task_work_ctx_reset(ctx);
-       /* bpf_mem_free expects migration to be disabled */
-       migrate_disable();
-       bpf_mem_free(&bpf_global_ma, ctx);
-       migrate_enable();
+       kfree_nolock(ctx);
 }
 
 static void bpf_task_work_ctx_free_rcu_tasks_trace(struct rcu_head *rcu)
@@ -4295,7 +4292,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
        if (ctx)
                return ctx;
 
-       ctx = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_task_work_ctx));
+       ctx = kmalloc_nolock(sizeof(struct bpf_task_work_ctx), 0, map->numa_node);
        if (!ctx)
                return ERR_PTR(-ENOMEM);
 
@@ -4309,7 +4306,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
                 * tw->ctx is set by concurrent BPF program, release allocated
                 * memory and try to reuse already set context.
                 */
-               bpf_mem_free(&bpf_global_ma, ctx);
+               kfree_nolock(ctx);
                return old_ctx;
        }
 
--

2.47.3

#6 lore raw

Re: [PATCH bpf-next v1 2/2] bpf: Use kmalloc_nolock for bpf_task_work_ctx allocation

Quoted 19 lines · Andrii Nakryiko
Quoted 8 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Replace bpf_mem_alloc/bpf_mem_free with kmalloc_nolock/kfree_nolock for allocating and freeing bpf_task_work_ctx. Now that kmalloc can be used from NMI context, it is the right generic mechanism for memory allocation in BPF kfuncs, even in restricted contexts like tracing programs.

Quoted 5 lines
---
 kernel/bpf/helpers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

LGTM

Quoted 8 lines

BTW, file dynptr is also still using bpf_mem_free, consider changing that to kmallock_nolock as a follow up as well, please (and I guess in general we should do an audit pass on what are the other bpf_mem_alloc places that should be converted, but Alexei is a more authoritative on what are the remaining constraints that would prevent some functionality to be switched to kmallock_nolock).

After reading 2nd patch I realized what 1st one is for. Let's first wait for sheaves and Harry's follow up to land:
https://lore.kernel.org/all/[email protected]/

then we don't need patches 1 and 2 and can just use kmalloc_nolock() to allocate task_work_ctx
and kfree_rcu() to free it with defer to irq_work when irqs disabled. kfree_rcu_nolock() will hopefully be introduced in 7.1 then we can clean it up even further.

#7 lore raw

Re: [PATCH bpf-next v1 2/2] bpf: Use kmalloc_nolock for bpf_task_work_ctx allocation

Quoted 30 lines · Alexei Starovoitov
Quoted 16 lines · Andrii Nakryiko
Quoted 7 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Replace bpf_mem_alloc/bpf_mem_free with kmalloc_nolock/kfree_nolock for allocating and freeing bpf_task_work_ctx. Now that kmalloc can be used from NMI context, it is the right generic mechanism for memory allocation in BPF kfuncs, even in restricted contexts like tracing programs.

Quoted 5 lines
---
  kernel/bpf/helpers.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)

LGTM

Quoted 8 lines

BTW, file dynptr is also still using bpf_mem_free, consider changing that to kmallock_nolock as a follow up as well, please (and I guess in general we should do an audit pass on what are the other bpf_mem_alloc places that should be converted, but Alexei is a more authoritative on what are the remaining constraints that would prevent some functionality to be switched to kmallock_nolock).

After reading 2nd patch I realized what 1st one is for. Let's first wait for sheaves and Harry's follow up to land:
https://lore.kernel.org/all/[email protected]/

Quoted 5 lines

then we don't need patches 1 and 2 and can just use kmalloc_nolock() to allocate task_work_ctx
and kfree_rcu() to free it with defer to irq_work when irqs disabled. kfree_rcu_nolock() will hopefully be introduced in 7.1 then we can clean it up even further.

Got it, thanks!