Skip to content

Commit 94515f3

Browse files
committed
Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Pull bpf fixes from Kumar Kartikeya Dwivedi: - Fix a UAF in socket clone early bailout paths (Matt Bobrowski) - Reject unhashed UDP sockets on sockmap update to prevent refcount leaks (Michal Luczaj) - Account for receive queue data in FIONREAD on sockmap sockets without a verdict program (Mattia Meleleo) - Reject negative constant offsets for verifier buffer pointers (Sun Jian) - Fix for tracing of kfuncs with implicit arguments (Ihor Solodrai) * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: selftests/bpf: Cover tracing implicit kfunc args bpf: Fix tracing of kfuncs with implicit args selftests/bpf: Cover negative buffer pointer offsets bpf: Reject negative const offsets for buffer pointers selftests/bpf: Test FIONREAD on a sockmap socket without a verdict program bpf, sockmap: Account for receive queue in FIONREAD without a verdict program selftests/bpf: Fail unbound UDP on sockmap update selftests/bpf: Adapt sockmap update error handling bpf, sockmap: Reject unhashed UDP sockets on sockmap update selftests/bpf: Ensure UDP sockets are bound bpf: Fix UAF in sock clone early bailouts
2 parents 8fc5743 + 3d84d67 commit 94515f3

18 files changed

Lines changed: 397 additions & 105 deletions

include/linux/btf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ const char *btf_str_by_offset(const struct btf *btf, u32 offset);
578578
struct btf *btf_parse_vmlinux(void);
579579
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
580580
u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog);
581+
int btf_kfunc_check_flag(const struct btf *btf, u32 kfunc_btf_id, u32 flag);
581582
bool btf_kfunc_is_allowed(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog);
582583
u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
583584
const struct bpf_prog *prog);

include/linux/skmsg.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,6 @@ static inline void psock_progs_drop(struct sk_psock_progs *progs)
551551
psock_set_prog(&progs->skb_verdict, NULL);
552552
}
553553

554-
/* for tcp only, sk is locked */
555-
static inline ssize_t sk_psock_msg_inq(struct sock *sk)
556-
{
557-
struct sk_psock *psock;
558-
ssize_t inq = 0;
559-
560-
psock = sk_psock_get(sk);
561-
if (likely(psock)) {
562-
inq = sk_psock_get_msg_len_nolock(psock);
563-
sk_psock_put(sk, psock);
564-
}
565-
return inq;
566-
}
567-
568554
/* for udp only, sk is not locked */
569555
static inline ssize_t sk_msg_first_len(struct sock *sk)
570556
{

kernel/bpf/btf.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9114,6 +9114,35 @@ u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_p
91149114
return btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);
91159115
}
91169116

9117+
/*
9118+
* Check a single KF_* @flag on a kfunc across all of its hook sets.
9119+
* Returns:
9120+
* * 1 if @flag is set
9121+
* * 0 if @flag is not set
9122+
* * -EINVAL if @flag is set inconsistently across the sets
9123+
* * -ENOENT if kfunc_btf_id is not a registered kfunc
9124+
*/
9125+
int btf_kfunc_check_flag(const struct btf *btf, u32 kfunc_btf_id, u32 flag)
9126+
{
9127+
enum btf_kfunc_hook hook;
9128+
int res = -ENOENT;
9129+
bool is_set;
9130+
u32 *flags;
9131+
9132+
for (hook = 0; hook < BTF_KFUNC_HOOK_MAX; hook++) {
9133+
flags = btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);
9134+
if (!flags)
9135+
continue;
9136+
is_set = *flags & flag;
9137+
if (res < 0)
9138+
res = is_set;
9139+
else if (res != is_set)
9140+
return -EINVAL;
9141+
}
9142+
9143+
return res;
9144+
}
9145+
91179146
u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
91189147
const struct bpf_prog *prog)
91199148
{

kernel/bpf/verifier.c

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,24 +2584,25 @@ static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset)
25842584

25852585
#define KF_IMPL_SUFFIX "_impl"
25862586

2587-
static const struct btf_type *find_kfunc_impl_proto(struct bpf_verifier_env *env,
2587+
static const struct btf_type *find_kfunc_impl_proto(struct bpf_verifier_log *log,
25882588
struct btf *btf,
25892589
const char *func_name)
25902590
{
2591-
char *buf = env->tmp_str_buf;
25922591
const struct btf_type *func;
2592+
char buf[KSYM_NAME_LEN];
25932593
s32 impl_id;
25942594
int len;
25952595

2596-
len = snprintf(buf, TMP_STR_BUF_LEN, "%s%s", func_name, KF_IMPL_SUFFIX);
2597-
if (len < 0 || len >= TMP_STR_BUF_LEN) {
2598-
verbose(env, "function name %s%s is too long\n", func_name, KF_IMPL_SUFFIX);
2596+
len = snprintf(buf, sizeof(buf), "%s%s", func_name, KF_IMPL_SUFFIX);
2597+
if (len < 0 || len >= sizeof(buf)) {
2598+
bpf_log(log, "function name %s%s is too long\n",
2599+
func_name, KF_IMPL_SUFFIX);
25992600
return NULL;
26002601
}
26012602

26022603
impl_id = btf_find_by_name_kind(btf, buf, BTF_KIND_FUNC);
26032604
if (impl_id <= 0) {
2604-
verbose(env, "cannot find function %s in BTF\n", buf);
2605+
bpf_log(log, "cannot find function %s in BTF\n", buf);
26052606
return NULL;
26062607
}
26072608

@@ -2653,7 +2654,7 @@ static int fetch_kfunc_meta(struct bpf_verifier_env *env,
26532654
* can be found through the counterpart _impl kfunc.
26542655
*/
26552656
if (kfunc_flags && (*kfunc_flags & KF_IMPLICIT_ARGS))
2656-
func_proto = find_kfunc_impl_proto(env, btf, func_name);
2657+
func_proto = find_kfunc_impl_proto(&env->log, btf, func_name);
26572658
else
26582659
func_proto = btf_type_by_id(btf, func->type);
26592660

@@ -5326,14 +5327,11 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
53265327
static int __check_buffer_access(struct bpf_verifier_env *env,
53275328
const char *buf_info,
53285329
const struct bpf_reg_state *reg,
5329-
argno_t argno, int off, int size)
5330+
argno_t argno, int off, int size,
5331+
u32 *access_end)
53305332
{
5331-
if (off < 0) {
5332-
verbose(env,
5333-
"%s invalid %s buffer access: off=%d, size=%d\n",
5334-
reg_arg_name(env, argno), buf_info, off, size);
5335-
return -EACCES;
5336-
}
5333+
s64 start;
5334+
53375335
if (!tnum_is_const(reg->var_off)) {
53385336
char tn_buf[48];
53395337

@@ -5344,21 +5342,30 @@ static int __check_buffer_access(struct bpf_verifier_env *env,
53445342
return -EACCES;
53455343
}
53465344

5345+
start = (s64)reg->var_off.value + off;
5346+
if (start < 0) {
5347+
verbose(env,
5348+
"%s invalid negative %s buffer offset: off=%d, var_off=%lld\n",
5349+
reg_arg_name(env, argno), buf_info, off, (s64)reg->var_off.value);
5350+
return -EACCES;
5351+
}
5352+
5353+
*access_end = start + size;
53475354
return 0;
53485355
}
53495356

53505357
static int check_tp_buffer_access(struct bpf_verifier_env *env,
53515358
const struct bpf_reg_state *reg,
53525359
argno_t argno, int off, int size)
53535360
{
5361+
u32 access_end;
53545362
int err;
53555363

5356-
err = __check_buffer_access(env, "tracepoint", reg, argno, off, size);
5364+
err = __check_buffer_access(env, "tracepoint", reg, argno, off, size, &access_end);
53575365
if (err)
53585366
return err;
53595367

5360-
env->prog->aux->max_tp_access = max(reg->var_off.value + off + size,
5361-
env->prog->aux->max_tp_access);
5368+
env->prog->aux->max_tp_access = max(access_end, env->prog->aux->max_tp_access);
53625369

53635370
return 0;
53645371
}
@@ -5370,13 +5377,14 @@ static int check_buffer_access(struct bpf_verifier_env *env,
53705377
u32 *max_access)
53715378
{
53725379
const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr";
5380+
u32 access_end;
53735381
int err;
53745382

5375-
err = __check_buffer_access(env, buf_info, reg, argno, off, size);
5383+
err = __check_buffer_access(env, buf_info, reg, argno, off, size, &access_end);
53765384
if (err)
53775385
return err;
53785386

5379-
*max_access = max(reg->var_off.value + off + size, *max_access);
5387+
*max_access = max(access_end, *max_access);
53805388

53815389
return 0;
53825390
}
@@ -18873,6 +18881,47 @@ static int btf_id_allow_sleepable(u32 btf_id, unsigned long addr, const struct b
1887318881
return -EINVAL;
1887418882
}
1887518883

18884+
/*
18885+
* Resolve the prototype describing a trace target's real ABI. A
18886+
* KF_IMPLICIT_ARGS kfunc has its injected args stripped from the public
18887+
* prototype, so use the _impl prototype; other targets use their own.
18888+
*/
18889+
static const struct btf_type *
18890+
btf_attach_func_proto(struct bpf_verifier_log *log, struct btf *btf, u32 func_id)
18891+
{
18892+
const struct btf_type *func;
18893+
struct module *mod = NULL;
18894+
const char *name;
18895+
int implicit;
18896+
18897+
func = btf_type_by_id(btf, func_id);
18898+
if (!func || !btf_type_is_func(func))
18899+
return NULL;
18900+
name = btf_name_by_offset(btf, func->name_off);
18901+
18902+
/*
18903+
* btf_kfunc_check_flag() reads kfunc_set_tab, which for a module is
18904+
* stable only once it is live; hold a module ref across the read to
18905+
* exclude a concurrent module load.
18906+
*/
18907+
if (btf_is_module(btf)) {
18908+
mod = btf_try_get_module(btf);
18909+
if (!mod)
18910+
return NULL;
18911+
}
18912+
implicit = btf_kfunc_check_flag(btf, func_id, KF_IMPLICIT_ARGS);
18913+
module_put(mod);
18914+
18915+
if (implicit == -EINVAL) {
18916+
bpf_log(log, "kfunc %s has inconsistent KF_IMPLICIT_ARGS\n", name);
18917+
return NULL;
18918+
}
18919+
if (implicit > 0)
18920+
return find_kfunc_impl_proto(log, btf, name);
18921+
18922+
return btf_type_by_id(btf, func->type);
18923+
}
18924+
1887618925
int bpf_check_attach_target(struct bpf_verifier_log *log,
1887718926
const struct bpf_prog *prog,
1887818927
const struct bpf_prog *tgt_prog,
@@ -19121,8 +19170,8 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
1912119170
if (prog_extension &&
1912219171
btf_check_type_match(log, prog, btf, t))
1912319172
return -EINVAL;
19124-
t = btf_type_by_id(btf, t->type);
19125-
if (!btf_type_is_func_proto(t))
19173+
t = btf_attach_func_proto(log, btf, btf_id);
19174+
if (!t || !btf_type_is_func_proto(t))
1912619175
return -EINVAL;
1912719176

1912819177
if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) &&
@@ -19405,10 +19454,8 @@ int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 bt
1940519454
tname = btf_name_by_offset(btf, t->name_off);
1940619455
if (!tname)
1940719456
return -EINVAL;
19408-
if (!btf_type_is_func(t))
19409-
return -EINVAL;
19410-
t = btf_type_by_id(btf, t->type);
19411-
if (!btf_type_is_func_proto(t))
19457+
t = btf_attach_func_proto(NULL, btf, btf_id);
19458+
if (!t || !btf_type_is_func_proto(t))
1941219459
return -EINVAL;
1941319460
err = btf_distill_func_proto(NULL, btf, t, tname, &tgt_info->fmodel);
1941419461
if (err < 0)

net/core/bpf_sk_storage.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
158158
struct bpf_local_storage_elem *selem;
159159
int ret = 0;
160160

161-
RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
162-
163161
rcu_read_lock_dont_migrate();
164162
sk_storage = rcu_dereference(sk->sk_bpf_storage);
165163

net/core/sock.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
24922492
sock_copy(newsk, sk);
24932493

24942494
newsk->sk_prot_creator = prot;
2495+
#ifdef CONFIG_BPF_SYSCALL
2496+
RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
2497+
#endif
24952498

24962499
/* SANITY */
24972500
if (likely(newsk->sk_net_refcnt)) {

net/core/sock_map.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
542542
{
543543
if (sk_is_tcp(sk))
544544
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
545+
if (sk_is_udp(sk))
546+
return sk_hashed(sk);
545547
if (sk_is_stream_unix(sk))
546548
return (1 << READ_ONCE(sk->sk_state)) & TCPF_ESTABLISHED;
547549
if (sk_is_vsock(sk) &&

net/ipv4/tcp_bpf.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
334334

335335
static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
336336
{
337+
struct sk_psock *psock;
337338
bool slow;
338339

339340
if (cmd != SIOCINQ)
@@ -344,7 +345,21 @@ static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
344345
return -EINVAL;
345346

346347
slow = lock_sock_fast(sk);
347-
*karg = sk_psock_msg_inq(sk);
348+
psock = sk_psock_get(sk);
349+
if (unlikely(!psock)) {
350+
unlock_sock_fast(sk, slow);
351+
return tcp_ioctl(sk, cmd, karg);
352+
}
353+
*karg = sk_psock_get_msg_len_nolock(psock);
354+
/* Without a verdict program, ingress data is never diverted to
355+
* ingress_msg: it stays in sk_receive_queue and is read through
356+
* the fallback to tcp_recvmsg(), so account for it like
357+
* tcp_ioctl() does.
358+
*/
359+
if (!READ_ONCE(psock->progs.stream_verdict) &&
360+
!READ_ONCE(psock->progs.skb_verdict))
361+
*karg += tcp_inq(sk);
362+
sk_psock_put(sk, psock);
348363
unlock_sock_fast(sk, slow);
349364

350365
return 0;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "kfunc_implicit_args_tracing.skel.h"
6+
7+
void test_kfunc_implicit_args_tracing(void)
8+
{
9+
struct kfunc_implicit_args_tracing *skel;
10+
LIBBPF_OPTS(bpf_test_run_opts, topts);
11+
int err, fd;
12+
13+
skel = kfunc_implicit_args_tracing__open_and_load();
14+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
15+
return;
16+
17+
err = kfunc_implicit_args_tracing__attach(skel);
18+
if (!ASSERT_OK(err, "attach"))
19+
goto cleanup;
20+
21+
fd = bpf_program__fd(skel->progs.trigger_implicit_arg);
22+
err = bpf_prog_test_run_opts(fd, &topts);
23+
if (!ASSERT_OK(err, "test_run"))
24+
goto cleanup;
25+
26+
ASSERT_EQ(topts.retval, 5, "kfunc_retval");
27+
ASSERT_EQ(skel->bss->fentry_arg_cnt, 2, "fentry_arg_cnt");
28+
ASSERT_NEQ(skel->bss->fentry_aux_arg, 0, "fentry_aux_arg");
29+
ASSERT_EQ(skel->bss->fentry_result, 1, "fentry_result");
30+
ASSERT_EQ(skel->bss->fexit_arg_cnt, 2, "fexit_arg_cnt");
31+
ASSERT_NEQ(skel->bss->fexit_aux_arg, 0, "fexit_aux_arg");
32+
ASSERT_EQ(skel->bss->fexit_result, 1, "fexit_result");
33+
34+
cleanup:
35+
kfunc_implicit_args_tracing__destroy(skel);
36+
}

0 commit comments

Comments
 (0)