Skip to content

Commit b0b1a85

Browse files
aspskAlexei Starovoitov
authored andcommitted
bpf: Add a map/btf from a fd array more consistently
The add_fd_from_fd_array() function takes a file descriptor as a parameter and tries to add either map or btf to the corresponding list of used objects. As was reported by Dan Carpenter, since the commit c81e432 ("bpf: Fix a potential use-after-free of BTF object"), the fdget() is called twice on the file descriptor, and thus userspace, potentially, can replace the file pointed to by the file descriptor in between the two calls. On practice, this shouldn't break anything on the kernel side, but for consistency fix the code such that only one fdget() is executed. Reported-by: Dan Carpenter <dan.carpenter@linaro.org> Closes: https://lore.kernel.org/r/aY689z7gHNv8rgVO@stanley.mountain/ Fixes: ccd2d79 ("bpf: Fix a potential use-after-free of BTF object") Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com> Link: https://lore.kernel.org/r/20260213212949.759321-1-a.s.protopopov@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent de516a9 commit b0b1a85

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25372,9 +25372,11 @@ static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd)
2537225372
return 0;
2537325373
}
2537425374

25375-
btf = btf_get_by_fd(fd);
25376-
if (!IS_ERR(btf))
25375+
btf = __btf_get_by_fd(f);
25376+
if (!IS_ERR(btf)) {
25377+
btf_get(btf);
2537725378
return __add_used_btf(env, btf);
25379+
}
2537825380

2537925381
verbose(env, "fd %d is not pointing to valid bpf_map or btf\n", fd);
2538025382
return PTR_ERR(map);

0 commit comments

Comments
 (0)