Skip to content

Commit 138c676

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Fix use-after-free of bpf_link when priming half-fails
If bpf_link_prime() succeeds to allocate new anon file, but then fails to allocate ID for it, link priming is considered to be failed and user is supposed ot be able to directly kfree() bpf_link, because it was never exposed to user-space. But at that point file already keeps a pointer to bpf_link and will eventually call bpf_link_release(), so if bpf_link was kfree()'d by caller, that would lead to use-after-free. Fix this by first allocating ID and only then allocating file. Adding ID to link_idr is ok, because link at that point still doesn't have its ID set, so no user-space process can create a new FD for it. Fixes: a3b80e1 ("bpf: Allocate ID for bpf_link") Reported-by: [email protected] Suggested-by: Martin KaFai Lau <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent beecf11 commit 138c676

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

kernel/bpf/syscall.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,19 +2348,20 @@ int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
23482348
if (fd < 0)
23492349
return fd;
23502350

2351-
file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2352-
if (IS_ERR(file)) {
2353-
put_unused_fd(fd);
2354-
return PTR_ERR(file);
2355-
}
23562351

23572352
id = bpf_link_alloc_id(link);
23582353
if (id < 0) {
23592354
put_unused_fd(fd);
2360-
fput(file);
23612355
return id;
23622356
}
23632357

2358+
file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2359+
if (IS_ERR(file)) {
2360+
bpf_link_free_id(id);
2361+
put_unused_fd(fd);
2362+
return PTR_ERR(file);
2363+
}
2364+
23642365
primer->link = link;
23652366
primer->file = file;
23662367
primer->fd = fd;

0 commit comments

Comments
 (0)