Skip to content

Commit 3d5ad2d

Browse files
committed
Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Pull bpf fixes from Daniel Borkmann: - Fix BPF verifier to not affect subreg_def marks in its range propagation (Eduard Zingerman) - Fix a truncation bug in the BPF verifier's handling of coerce_reg_to_size_sx (Dimitar Kanaliev) - Fix the BPF verifier's delta propagation between linked registers under 32-bit addition (Daniel Borkmann) - Fix a NULL pointer dereference in BPF devmap due to missing rxq information (Florian Kauer) - Fix a memory leak in bpf_core_apply (Jiri Olsa) - Fix an UBSAN-reported array-index-out-of-bounds in BTF parsing for arrays of nested structs (Hou Tao) - Fix build ID fetching where memory areas backing the file were created with memfd_secret (Andrii Nakryiko) - Fix BPF task iterator tid filtering which was incorrectly using pid instead of tid (Jordan Rome) - Several fixes for BPF sockmap and BPF sockhash redirection in combination with vsocks (Michal Luczaj) - Fix riscv BPF JIT and make BPF_CMPXCHG fully ordered (Andrea Parri) - Fix riscv BPF JIT under CONFIG_CFI_CLANG to prevent the possibility of an infinite BPF tailcall (Pu Lehui) - Fix a build warning from resolve_btfids that bpf_lsm_key_free cannot be resolved (Thomas Weißschuh) - Fix a bug in kfunc BTF caching for modules where the wrong BTF object was returned (Toke Høiland-Jørgensen) - Fix a BPF selftest compilation error in cgroup-related tests with musl libc (Tony Ambardar) - Several fixes to BPF link info dumps to fill missing fields (Tyrone Wu) - Add BPF selftests for kfuncs from multiple modules, checking that the correct kfuncs are called (Simon Sundberg) - Ensure that internal and user-facing bpf_redirect flags don't overlap (Toke Høiland-Jørgensen) - Switch to use kvzmalloc to allocate BPF verifier environment (Rik van Riel) - Use raw_spinlock_t in BPF ringbuf to fix a sleep in atomic splat under RT (Wander Lairson Costa) * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: (38 commits) lib/buildid: Handle memfd_secret() files in build_id_parse() selftests/bpf: Add test case for delta propagation bpf: Fix print_reg_state's constant scalar dump bpf: Fix incorrect delta propagation between linked registers bpf: Properly test iter/task tid filtering bpf: Fix iter/task tid filtering riscv, bpf: Make BPF_CMPXCHG fully ordered bpf, vsock: Drop static vsock_bpf_prot initialization vsock: Update msg_count on read_skb() vsock: Update rx_bytes on read_skb() bpf, sockmap: SK_DROP on attempted redirects of unsupported af_vsock selftests/bpf: Add asserts for netfilter link info bpf: Fix link info netfilter flags to populate defrag flag selftests/bpf: Add test for sign extension in coerce_subreg_to_size_sx() selftests/bpf: Add test for truncation after sign extension in coerce_reg_to_size_sx() bpf: Fix truncation bug in coerce_reg_to_size_sx() selftests/bpf: Assert link info uprobe_multi count & path_size if unset bpf: Fix unpopulated path_size when uprobe_multi fields unset selftests/bpf: Fix cross-compiling urandom_read selftests/bpf: Add test for kfunc module order ...
2 parents dbafedd + 5ac9b4e commit 3d5ad2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+847
-132
lines changed

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define RV_MAX_REG_ARGS 8
1919
#define RV_FENTRY_NINSNS 2
2020
#define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
21+
#define RV_KCFI_NINSNS (IS_ENABLED(CONFIG_CFI_CLANG) ? 1 : 0)
2122
/* imm that allows emit_imm to emit max count insns */
2223
#define RV_MAX_COUNT_IMM 0x7FFF7FF7FF7FF7FF
2324

@@ -271,7 +272,8 @@ static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
271272
if (!is_tail_call)
272273
emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
273274
emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
274-
is_tail_call ? (RV_FENTRY_NINSNS + 1) * 4 : 0, /* skip reserved nops and TCC init */
275+
/* kcfi, fentry and TCC init insns will be skipped on tailcall */
276+
is_tail_call ? (RV_KCFI_NINSNS + RV_FENTRY_NINSNS + 1) * 4 : 0,
275277
ctx);
276278
}
277279

@@ -548,8 +550,8 @@ static void emit_atomic(u8 rd, u8 rs, s16 off, s32 imm, bool is64,
548550
rv_lr_w(r0, 0, rd, 0, 0), ctx);
549551
jmp_offset = ninsns_rvoff(8);
550552
emit(rv_bne(RV_REG_T2, r0, jmp_offset >> 1), ctx);
551-
emit(is64 ? rv_sc_d(RV_REG_T3, rs, rd, 0, 0) :
552-
rv_sc_w(RV_REG_T3, rs, rd, 0, 0), ctx);
553+
emit(is64 ? rv_sc_d(RV_REG_T3, rs, rd, 0, 1) :
554+
rv_sc_w(RV_REG_T3, rs, rd, 0, 1), ctx);
553555
jmp_offset = ninsns_rvoff(-6);
554556
emit(rv_bne(RV_REG_T3, 0, jmp_offset >> 1), ctx);
555557
emit(rv_fence(0x3, 0x3), ctx);

include/net/sock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,11 @@ static inline bool sk_is_stream_unix(const struct sock *sk)
27172717
return sk->sk_family == AF_UNIX && sk->sk_type == SOCK_STREAM;
27182718
}
27192719

2720+
static inline bool sk_is_vsock(const struct sock *sk)
2721+
{
2722+
return sk->sk_family == AF_VSOCK;
2723+
}
2724+
27202725
/**
27212726
* sk_eat_skb - Release a skb if it is no longer needed
27222727
* @sk: socket to eat this skb from

include/uapi/linux/bpf.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6047,11 +6047,6 @@ enum {
60476047
BPF_F_MARK_ENFORCE = (1ULL << 6),
60486048
};
60496049

6050-
/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
6051-
enum {
6052-
BPF_F_INGRESS = (1ULL << 0),
6053-
};
6054-
60556050
/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
60566051
enum {
60576052
BPF_F_TUNINFO_IPV6 = (1ULL << 0),
@@ -6198,10 +6193,12 @@ enum {
61986193
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
61996194
};
62006195

6201-
/* Flags for bpf_redirect_map helper */
6196+
/* Flags for bpf_redirect and bpf_redirect_map helpers */
62026197
enum {
6203-
BPF_F_BROADCAST = (1ULL << 3),
6204-
BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
6198+
BPF_F_INGRESS = (1ULL << 0), /* used for skb path */
6199+
BPF_F_BROADCAST = (1ULL << 3), /* used for XDP path */
6200+
BPF_F_EXCLUDE_INGRESS = (1ULL << 4), /* used for XDP path */
6201+
#define BPF_F_REDIRECT_FLAGS (BPF_F_INGRESS | BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS)
62056202
};
62066203

62076204
#define __bpf_md_ptr(type, name) \

kernel/bpf/bpf_lsm.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,6 @@ BTF_ID(func, bpf_lsm_path_chmod)
339339
BTF_ID(func, bpf_lsm_path_chown)
340340
#endif /* CONFIG_SECURITY_PATH */
341341

342-
#ifdef CONFIG_KEYS
343-
BTF_ID(func, bpf_lsm_key_free)
344-
#endif /* CONFIG_KEYS */
345-
346342
BTF_ID(func, bpf_lsm_mmap_file)
347343
BTF_ID(func, bpf_lsm_netlink_send)
348344
BTF_ID(func, bpf_lsm_path_notify)

kernel/bpf/btf.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
35233523
* (i + 1) * elem_size
35243524
* where i is the repeat index and elem_size is the size of an element.
35253525
*/
3526-
static int btf_repeat_fields(struct btf_field_info *info,
3526+
static int btf_repeat_fields(struct btf_field_info *info, int info_cnt,
35273527
u32 field_cnt, u32 repeat_cnt, u32 elem_size)
35283528
{
35293529
u32 i, j;
@@ -3543,6 +3543,12 @@ static int btf_repeat_fields(struct btf_field_info *info,
35433543
}
35443544
}
35453545

3546+
/* The type of struct size or variable size is u32,
3547+
* so the multiplication will not overflow.
3548+
*/
3549+
if (field_cnt * (repeat_cnt + 1) > info_cnt)
3550+
return -E2BIG;
3551+
35463552
cur = field_cnt;
35473553
for (i = 0; i < repeat_cnt; i++) {
35483554
memcpy(&info[cur], &info[0], field_cnt * sizeof(info[0]));
@@ -3587,7 +3593,7 @@ static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *
35873593
info[i].off += off;
35883594

35893595
if (nelems > 1) {
3590-
err = btf_repeat_fields(info, ret, nelems - 1, t->size);
3596+
err = btf_repeat_fields(info, info_cnt, ret, nelems - 1, t->size);
35913597
if (err == 0)
35923598
ret *= nelems;
35933599
else
@@ -3681,10 +3687,10 @@ static int btf_find_field_one(const struct btf *btf,
36813687

36823688
if (ret == BTF_FIELD_IGNORE)
36833689
return 0;
3684-
if (nelems > info_cnt)
3690+
if (!info_cnt)
36853691
return -E2BIG;
36863692
if (nelems > 1) {
3687-
ret = btf_repeat_fields(info, 1, nelems - 1, sz);
3693+
ret = btf_repeat_fields(info, info_cnt, 1, nelems - 1, sz);
36883694
if (ret < 0)
36893695
return ret;
36903696
}
@@ -8961,6 +8967,7 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
89618967
if (!type) {
89628968
bpf_log(ctx->log, "relo #%u: bad type id %u\n",
89638969
relo_idx, relo->type_id);
8970+
kfree(specs);
89648971
return -EINVAL;
89658972
}
89668973

kernel/bpf/devmap.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ static int dev_map_hash_get_next_key(struct bpf_map *map, void *key,
333333

334334
static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,
335335
struct xdp_frame **frames, int n,
336-
struct net_device *dev)
336+
struct net_device *tx_dev,
337+
struct net_device *rx_dev)
337338
{
338-
struct xdp_txq_info txq = { .dev = dev };
339+
struct xdp_txq_info txq = { .dev = tx_dev };
340+
struct xdp_rxq_info rxq = { .dev = rx_dev };
339341
struct xdp_buff xdp;
340342
int i, nframes = 0;
341343

@@ -346,6 +348,7 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,
346348

347349
xdp_convert_frame_to_buff(xdpf, &xdp);
348350
xdp.txq = &txq;
351+
xdp.rxq = &rxq;
349352

350353
act = bpf_prog_run_xdp(xdp_prog, &xdp);
351354
switch (act) {
@@ -360,7 +363,7 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,
360363
bpf_warn_invalid_xdp_action(NULL, xdp_prog, act);
361364
fallthrough;
362365
case XDP_ABORTED:
363-
trace_xdp_exception(dev, xdp_prog, act);
366+
trace_xdp_exception(tx_dev, xdp_prog, act);
364367
fallthrough;
365368
case XDP_DROP:
366369
xdp_return_frame_rx_napi(xdpf);
@@ -388,7 +391,7 @@ static void bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags)
388391
}
389392

390393
if (bq->xdp_prog) {
391-
to_send = dev_map_bpf_prog_run(bq->xdp_prog, bq->q, cnt, dev);
394+
to_send = dev_map_bpf_prog_run(bq->xdp_prog, bq->q, cnt, dev, bq->dev_rx);
392395
if (!to_send)
393396
goto out;
394397
}

kernel/bpf/log.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,7 @@ static void print_reg_state(struct bpf_verifier_env *env,
688688
if (t == SCALAR_VALUE && reg->precise)
689689
verbose(env, "P");
690690
if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) {
691-
/* reg->off should be 0 for SCALAR_VALUE */
692-
verbose_snum(env, reg->var_off.value + reg->off);
691+
verbose_snum(env, reg->var_off.value);
693692
return;
694693
}
695694

kernel/bpf/ringbuf.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct bpf_ringbuf {
2929
u64 mask;
3030
struct page **pages;
3131
int nr_pages;
32-
spinlock_t spinlock ____cacheline_aligned_in_smp;
32+
raw_spinlock_t spinlock ____cacheline_aligned_in_smp;
3333
/* For user-space producer ring buffers, an atomic_t busy bit is used
3434
* to synchronize access to the ring buffers in the kernel, rather than
3535
* the spinlock that is used for kernel-producer ring buffers. This is
@@ -173,7 +173,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
173173
if (!rb)
174174
return NULL;
175175

176-
spin_lock_init(&rb->spinlock);
176+
raw_spin_lock_init(&rb->spinlock);
177177
atomic_set(&rb->busy, 0);
178178
init_waitqueue_head(&rb->waitq);
179179
init_irq_work(&rb->work, bpf_ringbuf_notify);
@@ -421,10 +421,10 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
421421
cons_pos = smp_load_acquire(&rb->consumer_pos);
422422

423423
if (in_nmi()) {
424-
if (!spin_trylock_irqsave(&rb->spinlock, flags))
424+
if (!raw_spin_trylock_irqsave(&rb->spinlock, flags))
425425
return NULL;
426426
} else {
427-
spin_lock_irqsave(&rb->spinlock, flags);
427+
raw_spin_lock_irqsave(&rb->spinlock, flags);
428428
}
429429

430430
pend_pos = rb->pending_pos;
@@ -450,7 +450,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
450450
*/
451451
if (new_prod_pos - cons_pos > rb->mask ||
452452
new_prod_pos - pend_pos > rb->mask) {
453-
spin_unlock_irqrestore(&rb->spinlock, flags);
453+
raw_spin_unlock_irqrestore(&rb->spinlock, flags);
454454
return NULL;
455455
}
456456

@@ -462,7 +462,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
462462
/* pairs with consumer's smp_load_acquire() */
463463
smp_store_release(&rb->producer_pos, new_prod_pos);
464464

465-
spin_unlock_irqrestore(&rb->spinlock, flags);
465+
raw_spin_unlock_irqrestore(&rb->spinlock, flags);
466466

467467
return (void *)hdr + BPF_RINGBUF_HDR_SZ;
468468
}

kernel/bpf/syscall.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,26 +3565,34 @@ static void bpf_perf_link_dealloc(struct bpf_link *link)
35653565
}
35663566

35673567
static int bpf_perf_link_fill_common(const struct perf_event *event,
3568-
char __user *uname, u32 ulen,
3568+
char __user *uname, u32 *ulenp,
35693569
u64 *probe_offset, u64 *probe_addr,
35703570
u32 *fd_type, unsigned long *missed)
35713571
{
35723572
const char *buf;
3573-
u32 prog_id;
3573+
u32 prog_id, ulen;
35743574
size_t len;
35753575
int err;
35763576

3577+
ulen = *ulenp;
35773578
if (!ulen ^ !uname)
35783579
return -EINVAL;
35793580

35803581
err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
35813582
probe_offset, probe_addr, missed);
35823583
if (err)
35833584
return err;
3585+
3586+
if (buf) {
3587+
len = strlen(buf);
3588+
*ulenp = len + 1;
3589+
} else {
3590+
*ulenp = 1;
3591+
}
35843592
if (!uname)
35853593
return 0;
3594+
35863595
if (buf) {
3587-
len = strlen(buf);
35883596
err = bpf_copy_to_user(uname, buf, ulen, len);
35893597
if (err)
35903598
return err;
@@ -3609,15 +3617,15 @@ static int bpf_perf_link_fill_kprobe(const struct perf_event *event,
36093617

36103618
uname = u64_to_user_ptr(info->perf_event.kprobe.func_name);
36113619
ulen = info->perf_event.kprobe.name_len;
3612-
err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr,
3620+
err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr,
36133621
&type, &missed);
36143622
if (err)
36153623
return err;
36163624
if (type == BPF_FD_TYPE_KRETPROBE)
36173625
info->perf_event.type = BPF_PERF_EVENT_KRETPROBE;
36183626
else
36193627
info->perf_event.type = BPF_PERF_EVENT_KPROBE;
3620-
3628+
info->perf_event.kprobe.name_len = ulen;
36213629
info->perf_event.kprobe.offset = offset;
36223630
info->perf_event.kprobe.missed = missed;
36233631
if (!kallsyms_show_value(current_cred()))
@@ -3639,7 +3647,7 @@ static int bpf_perf_link_fill_uprobe(const struct perf_event *event,
36393647

36403648
uname = u64_to_user_ptr(info->perf_event.uprobe.file_name);
36413649
ulen = info->perf_event.uprobe.name_len;
3642-
err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr,
3650+
err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr,
36433651
&type, NULL);
36443652
if (err)
36453653
return err;
@@ -3648,6 +3656,7 @@ static int bpf_perf_link_fill_uprobe(const struct perf_event *event,
36483656
info->perf_event.type = BPF_PERF_EVENT_URETPROBE;
36493657
else
36503658
info->perf_event.type = BPF_PERF_EVENT_UPROBE;
3659+
info->perf_event.uprobe.name_len = ulen;
36513660
info->perf_event.uprobe.offset = offset;
36523661
info->perf_event.uprobe.cookie = event->bpf_cookie;
36533662
return 0;
@@ -3673,12 +3682,18 @@ static int bpf_perf_link_fill_tracepoint(const struct perf_event *event,
36733682
{
36743683
char __user *uname;
36753684
u32 ulen;
3685+
int err;
36763686

36773687
uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name);
36783688
ulen = info->perf_event.tracepoint.name_len;
3689+
err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL);
3690+
if (err)
3691+
return err;
3692+
36793693
info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT;
3694+
info->perf_event.tracepoint.name_len = ulen;
36803695
info->perf_event.tracepoint.cookie = event->bpf_cookie;
3681-
return bpf_perf_link_fill_common(event, uname, ulen, NULL, NULL, NULL, NULL);
3696+
return 0;
36823697
}
36833698

36843699
static int bpf_perf_link_fill_perf_event(const struct perf_event *event,

kernel/bpf/task_iter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *co
9999
rcu_read_lock();
100100
pid = find_pid_ns(common->pid, common->ns);
101101
if (pid) {
102-
task = get_pid_task(pid, PIDTYPE_TGID);
102+
task = get_pid_task(pid, PIDTYPE_PID);
103103
*tid = common->pid;
104104
}
105105
rcu_read_unlock();

0 commit comments

Comments
 (0)