Skip to content

Commit 102acba

Browse files
keesAlexei Starovoitov
authored andcommitted
bpf: Replace callers of BPF_CAST_CALL with proper function typedef
In order to keep ahead of cases in the kernel where Control Flow Integrity (CFI) may trip over function call casts, enabling -Wcast-function-type is helpful. To that end, BPF_CAST_CALL causes various warnings and is one of the last places in the kernel triggering this warning. For actual function calls, replace BPF_CAST_CALL() with a typedef, which captures the same details about the given function pointers. This change results in no object code difference. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: KSPP/linux#20 Link: https://lore.kernel.org/lkml/CAEf4Bzb46=-J5Fxc3mMZ8JQPtK1uoE0q6+g6WPz53Cvx=CBEhw@mail.gmail.com Link: https://lore.kernel.org/bpf/20210928230946.4062144-3-keescook@chromium.org
1 parent 3d717fa commit 102acba

5 files changed

Lines changed: 11 additions & 17 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern struct idr btf_idr;
4848
extern spinlock_t btf_idr_lock;
4949
extern struct kobject *btf_kobj;
5050

51+
typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
5152
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
5253
struct bpf_iter_aux_info *aux);
5354
typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
@@ -142,7 +143,8 @@ struct bpf_map_ops {
142143
int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
143144
struct bpf_func_state *caller,
144145
struct bpf_func_state *callee);
145-
int (*map_for_each_callback)(struct bpf_map *map, void *callback_fn,
146+
int (*map_for_each_callback)(struct bpf_map *map,
147+
bpf_callback_t callback_fn,
146148
void *callback_ctx, u64 flags);
147149

148150
/* BTF name and id of struct allocated by map_alloc */

‎include/linux/filter.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,6 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
360360
.off = 0, \
361361
.imm = TGT })
362362

363-
/* Function call */
364-
365-
#define BPF_CAST_CALL(x) \
366-
((u64 (*)(u64, u64, u64, u64, u64))(x))
367-
368363
/* Convert function address to BPF immediate */
369364

370365
#define BPF_CALL_IMM(x) ((void *)(x) - (void *)__bpf_call_base)

‎kernel/bpf/arraymap.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ static const struct bpf_iter_seq_info iter_seq_info = {
645645
.seq_priv_size = sizeof(struct bpf_iter_seq_array_map_info),
646646
};
647647

648-
static int bpf_for_each_array_elem(struct bpf_map *map, void *callback_fn,
648+
static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_fn,
649649
void *callback_ctx, u64 flags)
650650
{
651651
u32 i, key, num_elems = 0;
@@ -668,9 +668,8 @@ static int bpf_for_each_array_elem(struct bpf_map *map, void *callback_fn,
668668
val = array->value + array->elem_size * i;
669669
num_elems++;
670670
key = i;
671-
ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
672-
(u64)(long)&key, (u64)(long)val,
673-
(u64)(long)callback_ctx, 0);
671+
ret = callback_fn((u64)(long)map, (u64)(long)&key,
672+
(u64)(long)val, (u64)(long)callback_ctx, 0);
674673
/* return value: 0 - continue, 1 - stop and return */
675674
if (ret)
676675
break;

‎kernel/bpf/hashtab.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,7 +2049,7 @@ static const struct bpf_iter_seq_info iter_seq_info = {
20492049
.seq_priv_size = sizeof(struct bpf_iter_seq_hash_map_info),
20502050
};
20512051

2052-
static int bpf_for_each_hash_elem(struct bpf_map *map, void *callback_fn,
2052+
static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_fn,
20532053
void *callback_ctx, u64 flags)
20542054
{
20552055
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
@@ -2089,9 +2089,8 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, void *callback_fn,
20892089
val = elem->key + roundup_key_size;
20902090
}
20912091
num_elems++;
2092-
ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
2093-
(u64)(long)key, (u64)(long)val,
2094-
(u64)(long)callback_ctx, 0);
2092+
ret = callback_fn((u64)(long)map, (u64)(long)key,
2093+
(u64)(long)val, (u64)(long)callback_ctx, 0);
20952094
/* return value: 0 - continue, 1 - stop and return */
20962095
if (ret) {
20972096
rcu_read_unlock();

‎kernel/bpf/helpers.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
10561056
struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
10571057
struct bpf_map *map = t->map;
10581058
void *value = t->value;
1059-
void *callback_fn;
1059+
bpf_callback_t callback_fn;
10601060
void *key;
10611061
u32 idx;
10621062

@@ -1081,8 +1081,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
10811081
key = value - round_up(map->key_size, 8);
10821082
}
10831083

1084-
BPF_CAST_CALL(callback_fn)((u64)(long)map, (u64)(long)key,
1085-
(u64)(long)value, 0, 0);
1084+
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
10861085
/* The verifier checked that return value is zero. */
10871086

10881087
this_cpu_write(hrtimer_running, NULL);

0 commit comments

Comments
 (0)