Skip to content

Commit a6923c0

Browse files
committed
Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Pull bpf fixes from Alexei Starovoitov: - Fix kCFI failures in JITed BPF code on arm64 (Sami Tolvanen, Puranjay Mohan, Mark Rutland, Maxwell Bland) - Disallow tail calls between BPF programs that use different cgroup local storage maps to prevent out-of-bounds access (Daniel Borkmann) - Fix unaligned access in flow_dissector and netfilter BPF programs (Paul Chaignon) - Avoid possible use of uninitialized mod_len in libbpf (Achill Gilgenast) * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: selftests/bpf: Test for unaligned flow_dissector ctx access bpf: Improve ctx access verifier error message bpf: Check netfilter ctx accesses are aligned bpf: Check flow_dissector ctx accesses are aligned arm64/cfi,bpf: Support kCFI + BPF on arm64 cfi: Move BPF CFI types and helpers to generic code cfi: add C CFI type macro libbpf: Avoid possible use of uninitialized mod_len bpf: Fix oob access in cgroup local storage bpf: Move cgroup iterator helpers to bpf.h bpf: Move bpf map owner out of common struct bpf: Add cookie object to bpf maps
2 parents f4f346c + d8d2d9d commit a6923c0

File tree

18 files changed

+229
-176
lines changed

18 files changed

+229
-176
lines changed

arch/arm64/include/asm/cfi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_ARM64_CFI_H
3+
#define _ASM_ARM64_CFI_H
4+
5+
#define __bpfcall
6+
7+
#endif /* _ASM_ARM64_CFI_H */

arch/arm64/net/bpf_jit_comp.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/arm-smccc.h>
1111
#include <linux/bitfield.h>
1212
#include <linux/bpf.h>
13+
#include <linux/cfi.h>
1314
#include <linux/filter.h>
1415
#include <linux/memory.h>
1516
#include <linux/printk.h>
@@ -114,6 +115,14 @@ static inline void emit(const u32 insn, struct jit_ctx *ctx)
114115
ctx->idx++;
115116
}
116117

118+
static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx)
119+
{
120+
if (ctx->image != NULL && ctx->write)
121+
ctx->image[ctx->idx] = data;
122+
123+
ctx->idx++;
124+
}
125+
117126
static inline void emit_a64_mov_i(const int is64, const int reg,
118127
const s32 val, struct jit_ctx *ctx)
119128
{
@@ -174,6 +183,12 @@ static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
174183
emit(insn, ctx);
175184
}
176185

186+
static inline void emit_kcfi(u32 hash, struct jit_ctx *ctx)
187+
{
188+
if (IS_ENABLED(CONFIG_CFI_CLANG))
189+
emit_u32_data(hash, ctx);
190+
}
191+
177192
/*
178193
* Kernel addresses in the vmalloc space use at most 48 bits, and the
179194
* remaining bits are guaranteed to be 0x1. So we can compose the address
@@ -503,7 +518,6 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
503518
const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
504519
const u8 priv_sp = bpf2a64[PRIVATE_SP];
505520
void __percpu *priv_stack_ptr;
506-
const int idx0 = ctx->idx;
507521
int cur_offset;
508522

509523
/*
@@ -529,6 +543,9 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
529543
*
530544
*/
531545

546+
emit_kcfi(is_main_prog ? cfi_bpf_hash : cfi_bpf_subprog_hash, ctx);
547+
const int idx0 = ctx->idx;
548+
532549
/* bpf function may be invoked by 3 instruction types:
533550
* 1. bl, attached via freplace to bpf prog via short jump
534551
* 2. br, attached via freplace to bpf prog via long jump
@@ -2146,9 +2163,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21462163
jit_data->ro_header = ro_header;
21472164
}
21482165

2149-
prog->bpf_func = (void *)ctx.ro_image;
2166+
prog->bpf_func = (void *)ctx.ro_image + cfi_get_offset();
21502167
prog->jited = 1;
2151-
prog->jited_len = prog_size;
2168+
prog->jited_len = prog_size - cfi_get_offset();
21522169

21532170
if (!prog->is_func || extra_pass) {
21542171
int i;
@@ -2527,6 +2544,12 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
25272544
/* return address locates above FP */
25282545
retaddr_off = stack_size + 8;
25292546

2547+
if (flags & BPF_TRAMP_F_INDIRECT) {
2548+
/*
2549+
* Indirect call for bpf_struct_ops
2550+
*/
2551+
emit_kcfi(cfi_get_func_hash(func_addr), ctx);
2552+
}
25302553
/* bpf trampoline may be invoked by 3 instruction types:
25312554
* 1. bl, attached to bpf prog or kernel function via short jump
25322555
* 2. br, attached to bpf prog or kernel function via long jump
@@ -3045,6 +3068,7 @@ void bpf_jit_free(struct bpf_prog *prog)
30453068
sizeof(jit_data->header->size));
30463069
kfree(jit_data);
30473070
}
3071+
prog->bpf_func -= cfi_get_offset();
30483072
hdr = bpf_jit_binary_pack_hdr(prog);
30493073
bpf_jit_binary_pack_free(hdr, NULL);
30503074
priv_stack_ptr = prog->aux->priv_stack_ptr;

arch/riscv/include/asm/cfi.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,11 @@ struct pt_regs;
1414
#ifdef CONFIG_CFI_CLANG
1515
enum bug_trap_type handle_cfi_failure(struct pt_regs *regs);
1616
#define __bpfcall
17-
static inline int cfi_get_offset(void)
18-
{
19-
return 4;
20-
}
21-
22-
#define cfi_get_offset cfi_get_offset
23-
extern u32 cfi_bpf_hash;
24-
extern u32 cfi_bpf_subprog_hash;
25-
extern u32 cfi_get_func_hash(void *func);
2617
#else
2718
static inline enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
2819
{
2920
return BUG_TRAP_TYPE_NONE;
3021
}
31-
32-
#define cfi_bpf_hash 0U
33-
#define cfi_bpf_subprog_hash 0U
34-
static inline u32 cfi_get_func_hash(void *func)
35-
{
36-
return 0;
37-
}
3822
#endif /* CONFIG_CFI_CLANG */
3923

4024
#endif /* _ASM_RISCV_CFI_H */

arch/riscv/kernel/cfi.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -75,56 +75,3 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
7575

7676
return report_cfi_failure(regs, regs->epc, &target, type);
7777
}
78-
79-
#ifdef CONFIG_CFI_CLANG
80-
struct bpf_insn;
81-
82-
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
83-
extern unsigned int __bpf_prog_runX(const void *ctx,
84-
const struct bpf_insn *insn);
85-
86-
/*
87-
* Force a reference to the external symbol so the compiler generates
88-
* __kcfi_typid.
89-
*/
90-
__ADDRESSABLE(__bpf_prog_runX);
91-
92-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
93-
asm (
94-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
95-
" .type cfi_bpf_hash,@object \n"
96-
" .globl cfi_bpf_hash \n"
97-
" .p2align 2, 0x0 \n"
98-
"cfi_bpf_hash: \n"
99-
" .word __kcfi_typeid___bpf_prog_runX \n"
100-
" .size cfi_bpf_hash, 4 \n"
101-
" .popsection \n"
102-
);
103-
104-
/* Must match bpf_callback_t */
105-
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
106-
107-
__ADDRESSABLE(__bpf_callback_fn);
108-
109-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
110-
asm (
111-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
112-
" .type cfi_bpf_subprog_hash,@object \n"
113-
" .globl cfi_bpf_subprog_hash \n"
114-
" .p2align 2, 0x0 \n"
115-
"cfi_bpf_subprog_hash: \n"
116-
" .word __kcfi_typeid___bpf_callback_fn \n"
117-
" .size cfi_bpf_subprog_hash, 4 \n"
118-
" .popsection \n"
119-
);
120-
121-
u32 cfi_get_func_hash(void *func)
122-
{
123-
u32 hash;
124-
125-
if (get_kernel_nofault(hash, func - cfi_get_offset()))
126-
return 0;
127-
128-
return hash;
129-
}
130-
#endif

arch/x86/include/asm/cfi.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ struct pt_regs;
116116
#ifdef CONFIG_CFI_CLANG
117117
enum bug_trap_type handle_cfi_failure(struct pt_regs *regs);
118118
#define __bpfcall
119-
extern u32 cfi_bpf_hash;
120-
extern u32 cfi_bpf_subprog_hash;
121119

122120
static inline int cfi_get_offset(void)
123121
{
@@ -135,6 +133,8 @@ static inline int cfi_get_offset(void)
135133
#define cfi_get_offset cfi_get_offset
136134

137135
extern u32 cfi_get_func_hash(void *func);
136+
#define cfi_get_func_hash cfi_get_func_hash
137+
138138
extern int cfi_get_func_arity(void *func);
139139

140140
#ifdef CONFIG_FINEIBT
@@ -153,12 +153,6 @@ static inline enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
153153
{
154154
return BUG_TRAP_TYPE_NONE;
155155
}
156-
#define cfi_bpf_hash 0U
157-
#define cfi_bpf_subprog_hash 0U
158-
static inline u32 cfi_get_func_hash(void *func)
159-
{
160-
return 0;
161-
}
162156
static inline int cfi_get_func_arity(void *func)
163157
{
164158
return 0;

arch/x86/kernel/alternative.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,43 +1184,6 @@ bool cfi_bhi __ro_after_init = false;
11841184
#endif
11851185

11861186
#ifdef CONFIG_CFI_CLANG
1187-
struct bpf_insn;
1188-
1189-
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
1190-
extern unsigned int __bpf_prog_runX(const void *ctx,
1191-
const struct bpf_insn *insn);
1192-
1193-
KCFI_REFERENCE(__bpf_prog_runX);
1194-
1195-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
1196-
asm (
1197-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
1198-
" .type cfi_bpf_hash,@object \n"
1199-
" .globl cfi_bpf_hash \n"
1200-
" .p2align 2, 0x0 \n"
1201-
"cfi_bpf_hash: \n"
1202-
" .long __kcfi_typeid___bpf_prog_runX \n"
1203-
" .size cfi_bpf_hash, 4 \n"
1204-
" .popsection \n"
1205-
);
1206-
1207-
/* Must match bpf_callback_t */
1208-
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
1209-
1210-
KCFI_REFERENCE(__bpf_callback_fn);
1211-
1212-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
1213-
asm (
1214-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
1215-
" .type cfi_bpf_subprog_hash,@object \n"
1216-
" .globl cfi_bpf_subprog_hash \n"
1217-
" .p2align 2, 0x0 \n"
1218-
"cfi_bpf_subprog_hash: \n"
1219-
" .long __kcfi_typeid___bpf_callback_fn \n"
1220-
" .size cfi_bpf_subprog_hash, 4 \n"
1221-
" .popsection \n"
1222-
);
1223-
12241187
u32 cfi_get_func_hash(void *func)
12251188
{
12261189
u32 hash;

include/linux/bpf-cgroup.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ to_cgroup_bpf_attach_type(enum bpf_attach_type attach_type)
7777
extern struct static_key_false cgroup_bpf_enabled_key[MAX_CGROUP_BPF_ATTACH_TYPE];
7878
#define cgroup_bpf_enabled(atype) static_branch_unlikely(&cgroup_bpf_enabled_key[atype])
7979

80-
#define for_each_cgroup_storage_type(stype) \
81-
for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
82-
8380
struct bpf_cgroup_storage_map;
8481

8582
struct bpf_storage_buffer {
@@ -510,8 +507,6 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
510507
#define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
511508
kernel_optval) ({ 0; })
512509

513-
#define for_each_cgroup_storage_type(stype) for (; false; )
514-
515510
#endif /* CONFIG_CGROUP_BPF */
516511

517512
#endif /* _BPF_CGROUP_H */

include/linux/bpf.h

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ enum btf_field_type {
208208
BPF_RES_SPIN_LOCK = (1 << 12),
209209
};
210210

211+
enum bpf_cgroup_storage_type {
212+
BPF_CGROUP_STORAGE_SHARED,
213+
BPF_CGROUP_STORAGE_PERCPU,
214+
__BPF_CGROUP_STORAGE_MAX
215+
#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
216+
};
217+
218+
#ifdef CONFIG_CGROUP_BPF
219+
# define for_each_cgroup_storage_type(stype) \
220+
for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
221+
#else
222+
# define for_each_cgroup_storage_type(stype) for (; false; )
223+
#endif /* CONFIG_CGROUP_BPF */
224+
211225
typedef void (*btf_dtor_kfunc_t)(void *);
212226

213227
struct btf_field_kptr {
@@ -260,6 +274,19 @@ struct bpf_list_node_kern {
260274
void *owner;
261275
} __attribute__((aligned(8)));
262276

277+
/* 'Ownership' of program-containing map is claimed by the first program
278+
* that is going to use this map or by the first program which FD is
279+
* stored in the map to make sure that all callers and callees have the
280+
* same prog type, JITed flag and xdp_has_frags flag.
281+
*/
282+
struct bpf_map_owner {
283+
enum bpf_prog_type type;
284+
bool jited;
285+
bool xdp_has_frags;
286+
u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE];
287+
const struct btf_type *attach_func_proto;
288+
};
289+
263290
struct bpf_map {
264291
const struct bpf_map_ops *ops;
265292
struct bpf_map *inner_map_meta;
@@ -292,24 +319,15 @@ struct bpf_map {
292319
struct rcu_head rcu;
293320
};
294321
atomic64_t writecnt;
295-
/* 'Ownership' of program-containing map is claimed by the first program
296-
* that is going to use this map or by the first program which FD is
297-
* stored in the map to make sure that all callers and callees have the
298-
* same prog type, JITed flag and xdp_has_frags flag.
299-
*/
300-
struct {
301-
const struct btf_type *attach_func_proto;
302-
spinlock_t lock;
303-
enum bpf_prog_type type;
304-
bool jited;
305-
bool xdp_has_frags;
306-
} owner;
322+
spinlock_t owner_lock;
323+
struct bpf_map_owner *owner;
307324
bool bypass_spec_v1;
308325
bool frozen; /* write-once; write-protected by freeze_mutex */
309326
bool free_after_mult_rcu_gp;
310327
bool free_after_rcu_gp;
311328
atomic64_t sleepable_refcnt;
312329
s64 __percpu *elem_count;
330+
u64 cookie; /* write-once */
313331
};
314332

315333
static inline const char *btf_field_type_name(enum btf_field_type type)
@@ -1082,14 +1100,6 @@ struct bpf_prog_offload {
10821100
u32 jited_len;
10831101
};
10841102

1085-
enum bpf_cgroup_storage_type {
1086-
BPF_CGROUP_STORAGE_SHARED,
1087-
BPF_CGROUP_STORAGE_PERCPU,
1088-
__BPF_CGROUP_STORAGE_MAX
1089-
};
1090-
1091-
#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
1092-
10931103
/* The longest tracepoint has 12 args.
10941104
* See include/trace/bpf_probe.h
10951105
*/
@@ -2108,6 +2118,16 @@ static inline bool bpf_map_flags_access_ok(u32 access_flags)
21082118
(BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
21092119
}
21102120

2121+
static inline struct bpf_map_owner *bpf_map_owner_alloc(struct bpf_map *map)
2122+
{
2123+
return kzalloc(sizeof(*map->owner), GFP_ATOMIC);
2124+
}
2125+
2126+
static inline void bpf_map_owner_free(struct bpf_map *map)
2127+
{
2128+
kfree(map->owner);
2129+
}
2130+
21112131
struct bpf_event_entry {
21122132
struct perf_event *event;
21132133
struct file *perf_file;

0 commit comments

Comments
 (0)