Skip to content

Commit ccd2d79

Browse files
aspskAlexei Starovoitov
authored andcommitted
bpf: Fix a potential use-after-free of BTF object
Refcounting in the check_pseudo_btf_id() function is incorrect: the __check_pseudo_btf_id() function might get called with a zero refcounted btf. Fix this, and patch related code accordingly. v3: rephrase a comment (AI) v2: fix a refcount leak introduced in v1 (AI) Reported-by: syzbot+5a0f1995634f7c1dadbf@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=5a0f1995634f7c1dadbf Fixes: 76145f7 ("bpf: Refactor check_pseudo_btf_id") Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com> Link: https://lore.kernel.org/r/20260209132904.63908-1-a.s.protopopov@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 04999b9 commit ccd2d79

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21333,29 +21333,29 @@ static int find_btf_percpu_datasec(struct btf *btf)
2133321333
}
2133421334

2133521335
/*
21336-
* Add btf to the used_btfs array and return the index. (If the btf was
21337-
* already added, then just return the index.) Upon successful insertion
21338-
* increase btf refcnt, and, if present, also refcount the corresponding
21339-
* kernel module.
21336+
* Add btf to the env->used_btfs array. If needed, refcount the
21337+
* corresponding kernel module. To simplify caller's logic
21338+
* in case of error or if btf was added before the function
21339+
* decreases the btf refcount.
2134021340
*/
2134121341
static int __add_used_btf(struct bpf_verifier_env *env, struct btf *btf)
2134221342
{
2134321343
struct btf_mod_pair *btf_mod;
21344+
int ret = 0;
2134421345
int i;
2134521346

2134621347
/* check whether we recorded this BTF (and maybe module) already */
2134721348
for (i = 0; i < env->used_btf_cnt; i++)
2134821349
if (env->used_btfs[i].btf == btf)
21349-
return i;
21350+
goto ret_put;
2135021351

2135121352
if (env->used_btf_cnt >= MAX_USED_BTFS) {
2135221353
verbose(env, "The total number of btfs per program has reached the limit of %u\n",
2135321354
MAX_USED_BTFS);
21354-
return -E2BIG;
21355+
ret = -E2BIG;
21356+
goto ret_put;
2135521357
}
2135621358

21357-
btf_get(btf);
21358-
2135921359
btf_mod = &env->used_btfs[env->used_btf_cnt];
2136021360
btf_mod->btf = btf;
2136121361
btf_mod->module = NULL;
@@ -21364,12 +21364,18 @@ static int __add_used_btf(struct bpf_verifier_env *env, struct btf *btf)
2136421364
if (btf_is_module(btf)) {
2136521365
btf_mod->module = btf_try_get_module(btf);
2136621366
if (!btf_mod->module) {
21367-
btf_put(btf);
21368-
return -ENXIO;
21367+
ret = -ENXIO;
21368+
goto ret_put;
2136921369
}
2137021370
}
2137121371

21372-
return env->used_btf_cnt++;
21372+
env->used_btf_cnt++;
21373+
return 0;
21374+
21375+
ret_put:
21376+
/* Either error or this BTF was already added */
21377+
btf_put(btf);
21378+
return ret;
2137321379
}
2137421380

2137521381
/* replace pseudo btf_id with kernel symbol address */
@@ -21466,9 +21472,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
2146621472

2146721473
btf_fd = insn[1].imm;
2146821474
if (btf_fd) {
21469-
CLASS(fd, f)(btf_fd);
21470-
21471-
btf = __btf_get_by_fd(f);
21475+
btf = btf_get_by_fd(btf_fd);
2147221476
if (IS_ERR(btf)) {
2147321477
verbose(env, "invalid module BTF object FD specified.\n");
2147421478
return -EINVAL;
@@ -21478,17 +21482,17 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
2147821482
verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n");
2147921483
return -EINVAL;
2148021484
}
21485+
btf_get(btf_vmlinux);
2148121486
btf = btf_vmlinux;
2148221487
}
2148321488

2148421489
err = __check_pseudo_btf_id(env, insn, aux, btf);
21485-
if (err)
21490+
if (err) {
21491+
btf_put(btf);
2148621492
return err;
21493+
}
2148721494

21488-
err = __add_used_btf(env, btf);
21489-
if (err < 0)
21490-
return err;
21491-
return 0;
21495+
return __add_used_btf(env, btf);
2149221496
}
2149321497

2149421498
static bool is_tracing_prog_type(enum bpf_prog_type type)
@@ -25368,13 +25372,9 @@ static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd)
2536825372
return 0;
2536925373
}
2537025374

25371-
btf = __btf_get_by_fd(f);
25372-
if (!IS_ERR(btf)) {
25373-
err = __add_used_btf(env, btf);
25374-
if (err < 0)
25375-
return err;
25376-
return 0;
25377-
}
25375+
btf = btf_get_by_fd(fd);
25376+
if (!IS_ERR(btf))
25377+
return __add_used_btf(env, btf);
2537825378

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

0 commit comments

Comments
 (0)