Skip to content

Commit 113a19b

Browse files
committed
bpf: put bpf_link's program when link is safe to be deallocated
JIRA: https://issues.redhat.com/browse/RHEL-85485 JIRA: https://issues.redhat.com/browse/RHEL-78741 CVE: CVE-2024-56786 commit f44ec87 Author: Andrii Nakryiko <andrii@kernel.org> Date: Fri Nov 1 11:17:52 2024 -0700 bpf: put bpf_link's program when link is safe to be deallocated In general, BPF link's underlying BPF program should be considered to be reachable through attach hook -> link -> prog chain, and, pessimistically, we have to assume that as long as link's memory is not safe to free, attach hook's code might hold a pointer to BPF program and use it. As such, it's not (generally) correct to put link's program early before waiting for RCU GPs to go through. More eager bpf_prog_put() that we currently do is mostly correct due to BPF program's release code doing similar RCU GP waiting, but as will be shown in the following patches, BPF program can be non-sleepable (and, thus, reliant on only "classic" RCU GP), while BPF link's attach hook can have sleepable semantics and needs to be protected by RCU Tasks Trace, and for such cases BPF link has to go through RCU Tasks Trace + "classic" RCU GPs before being deallocated. And so, if we put BPF program early, we might free BPF program before we free BPF link, leading to use-after-free situation. So, this patch defers bpf_prog_put() until we are ready to perform bpf_link's deallocation. At worst, this delays BPF program freeing by one extra RCU GP, but that seems completely acceptable. Alternatively, we'd need more elaborate ways to determine BPF hook, BPF link, and BPF program lifetimes, and how they relate to each other, which seems like an unnecessary complication. Note, for most BPF links we still will perform eager bpf_prog_put() and link dealloc, so for those BPF links there are no observable changes whatsoever. Only BPF links that use deferred dealloc might notice slightly delayed freeing of BPF programs. Also, to reduce code and logic duplication, extract program put + link dealloc logic into bpf_link_dealloc() helper. Link: https://lore.kernel.org/20241101181754.782341-1-andrii@kernel.org Tested-by: Jordan Rife <jrife@google.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent aba0370 commit 113a19b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

kernel/bpf/syscall.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,12 +3057,24 @@ void bpf_link_inc(struct bpf_link *link)
30573057
atomic64_inc(&link->refcnt);
30583058
}
30593059

3060+
static void bpf_link_dealloc(struct bpf_link *link)
3061+
{
3062+
/* now that we know that bpf_link itself can't be reached, put underlying BPF program */
3063+
if (link->prog)
3064+
bpf_prog_put(link->prog);
3065+
3066+
/* free bpf_link and its containing memory */
3067+
if (link->ops->dealloc_deferred)
3068+
link->ops->dealloc_deferred(link);
3069+
else
3070+
link->ops->dealloc(link);
3071+
}
3072+
30603073
static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu)
30613074
{
30623075
struct bpf_link *link = container_of(rcu, struct bpf_link, rcu);
30633076

3064-
/* free bpf_link and its containing memory */
3065-
link->ops->dealloc_deferred(link);
3077+
bpf_link_dealloc(link);
30663078
}
30673079

30683080
static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu)
@@ -3084,7 +3096,6 @@ static void bpf_link_free(struct bpf_link *link)
30843096
sleepable = link->prog->sleepable;
30853097
/* detach BPF program, clean up used resources */
30863098
ops->release(link);
3087-
bpf_prog_put(link->prog);
30883099
}
30893100
if (ops->dealloc_deferred) {
30903101
/* schedule BPF link deallocation; if underlying BPF program
@@ -3095,8 +3106,9 @@ static void bpf_link_free(struct bpf_link *link)
30953106
call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
30963107
else
30973108
call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
3098-
} else if (ops->dealloc)
3099-
ops->dealloc(link);
3109+
} else if (ops->dealloc) {
3110+
bpf_link_dealloc(link);
3111+
}
31003112
}
31013113

31023114
static void bpf_link_put_deferred(struct work_struct *work)

0 commit comments

Comments
 (0)