public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [bug report] bpf: Fix a potential use-after-free of BTF object
       [not found] <caa37f28-a2e8-4e0a-a9ce-a365ce805e4b@stanley.mountain>
@ 2026-02-13  5:56 ` Dan Carpenter
  2026-02-13 10:29   ` Anton Protopopov
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2026-02-13  5:56 UTC (permalink / raw)
  To: Anton Protopopov; +Cc: bpf, linux-kernel

[ Smatch checking is paused while we raise funding. #SadFace
  https://lore.kernel.org/all/aTaiGSbWZ9DJaGo7@stanley.mountain/ -dan ]

Hello Anton Protopopov,

Commit c81e4322acf0 ("bpf: Fix a potential use-after-free of BTF
object") from Feb 9, 2026 (linux-next), leads to the following Smatch
static checker warning:

	kernel/bpf/verifier.c:25375 add_fd_from_fd_array()
	warn: double fget(): 'fd'

kernel/bpf/verifier.c
    25360 static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd)
    25361 {
    25362         struct bpf_map *map;
    25363         struct btf *btf;
    25364         CLASS(fd, f)(fd);

This assigns f = fdget(fd);

    25365         int err;
    25366 
    25367         map = __bpf_map_get(f);
    25368         if (!IS_ERR(map)) {
    25369                 err = __add_used_map(env, map);
    25370                 if (err < 0)
    25371                         return err;
    25372                 return 0;
    25373         }
    25374 
--> 25375         btf = btf_get_by_fd(fd);
                                      ^^
This re-uses the fd.  The reason behind the warning is that the user
could have changed the fd to point to a different file from the
start of the function.

    25376         if (!IS_ERR(btf))
    25377                 return __add_used_btf(env, btf);
    25378 
    25379         verbose(env, "fd %d is not pointing to valid bpf_map or btf\n", fd);
    25380         return PTR_ERR(map);
    25381 }

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [bug report] bpf: Fix a potential use-after-free of BTF object
  2026-02-13  5:56 ` [bug report] bpf: Fix a potential use-after-free of BTF object Dan Carpenter
@ 2026-02-13 10:29   ` Anton Protopopov
  0 siblings, 0 replies; 2+ messages in thread
From: Anton Protopopov @ 2026-02-13 10:29 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: bpf, linux-kernel

On 26/02/13 08:56AM, Dan Carpenter wrote:
> [ Smatch checking is paused while we raise funding. #SadFace
>   https://lore.kernel.org/all/aTaiGSbWZ9DJaGo7@stanley.mountain/ -dan ]
> 
> Hello Anton Protopopov,
> 
> Commit c81e4322acf0 ("bpf: Fix a potential use-after-free of BTF
> object") from Feb 9, 2026 (linux-next), leads to the following Smatch
> static checker warning:
> 
> 	kernel/bpf/verifier.c:25375 add_fd_from_fd_array()
> 	warn: double fget(): 'fd'
> 
> kernel/bpf/verifier.c
>     25360 static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd)
>     25361 {
>     25362         struct bpf_map *map;
>     25363         struct btf *btf;
>     25364         CLASS(fd, f)(fd);
> 
> This assigns f = fdget(fd);
> 
>     25365         int err;
>     25366 
>     25367         map = __bpf_map_get(f);
>     25368         if (!IS_ERR(map)) {
>     25369                 err = __add_used_map(env, map);
>     25370                 if (err < 0)
>     25371                         return err;
>     25372                 return 0;
>     25373         }
>     25374 
> --> 25375         btf = btf_get_by_fd(fd);
>                                       ^^
> This re-uses the fd.  The reason behind the warning is that the user
> could have changed the fd to point to a different file from the
> start of the function.

True, this could happen. Not sure this is a real problem (if a user
replaced this by a valid BTF, well...)

>     25376         if (!IS_ERR(btf))
>     25377                 return __add_used_btf(env, btf);

The problem with this piece of code is that originally I wanted to
keep naming/appearance in sync, but the corresponding map/btf
functions, historically, behave a bit different...

To keep things working and to address the bug report in this
thread, one fix is required:

-       btf = btf_get_by_fd(fd);
-       if (!IS_ERR(btf))
+       btf = __btf_get_by_fd(f);
+       if (!IS_ERR(btf)) {
+               btf_get(btf);
                return __add_used_btf(env, btf);
+       }

I will send this fix later.

>     25379         verbose(env, "fd %d is not pointing to valid bpf_map or btf\n", fd);
>     25380         return PTR_ERR(map);
>     25381 }
> 
> regards,
> dan carpenter

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-13 10:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <caa37f28-a2e8-4e0a-a9ce-a365ce805e4b@stanley.mountain>
2026-02-13  5:56 ` [bug report] bpf: Fix a potential use-after-free of BTF object Dan Carpenter
2026-02-13 10:29   ` Anton Protopopov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox