]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Fix memory leak of bpf_scc_info objects
authorEduard Zingerman <eddyz87@gmail.com>
Fri, 1 Aug 2025 23:23:30 +0000 (16:23 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 2 Aug 2025 16:04:57 +0000 (09:04 -0700)
env->scc_info array contains references to bpf_scc_info objects
allocated lazily in verifier.c:scc_visit_alloc().
env->scc_cnt was supposed to track env->scc_info array size
in order to free referenced objects in verifier.c:free_states().
Fix initialization of env->scc_cnt that was omitted in
verifier.c:compute_scc().

To reproduce the bug:
- build with CONFIG_DEBUG_KMEMLEAK
- boot and load bpf program with loops, e.g.:
  ./veristat -q pyperf180.bpf.o
- initiate memleak scan and check results:
  echo scan > /sys/kernel/debug/kmemleak
  cat /sys/kernel/debug/kmemleak

Fixes: c9e31900b54c ("bpf: propagate read/precision marks over state graph backedges")
Reported-by: Jens Axboe <axboe@kernel.dk>
Closes: https://lore.kernel.org/bpf/CAADnVQKXUWg9uRCPD5ebRXwN4dmBCRUFFM7kN=GxymYz3zU25A@mail.gmail.com/T/
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Tested-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20250801232330.1800436-1-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index 0806295945e4006bae53513570f1c75545228c8d..c4f69a9e9af6904d21177c851bb059541ba98057 100644 (file)
@@ -23114,6 +23114,8 @@ static void free_states(struct bpf_verifier_env *env)
 
        for (i = 0; i < env->scc_cnt; ++i) {
                info = env->scc_info[i];
+               if (!info)
+                       continue;
                for (j = 0; j < info->num_visits; j++)
                        free_backedges(&info->visits[j]);
                kvfree(info);
@@ -24554,6 +24556,7 @@ dfs_continue:
                err = -ENOMEM;
                goto exit;
        }
+       env->scc_cnt = next_scc_id;
 exit:
        kvfree(stack);
        kvfree(pre);