Skip to content

Commit dd2260e

Browse files
tohojogregkh
authored andcommitted
bpf: Fix potential race in tail call compatibility check
commit 54713c8 upstream. Lorenzo noticed that the code testing for program type compatibility of tail call maps is potentially racy in that two threads could encounter a map with an unset type simultaneously and both return true even though they are inserting incompatible programs. The race window is quite small, but artificially enlarging it by adding a usleep_range() inside the check in bpf_prog_array_compatible() makes it trivial to trigger from userspace with a program that does, essentially: map_fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, 4, 4, 2, 0); pid = fork(); if (pid) { key = 0; value = xdp_fd; } else { key = 1; value = tc_fd; } err = bpf_map_update_elem(map_fd, &key, &value, 0); While the race window is small, it has potentially serious ramifications in that triggering it would allow a BPF program to tail call to a program of a different type. So let's get rid of it by protecting the update with a spinlock. The commit in the Fixes tag is the last commit that touches the code in question. v2: - Use a spinlock instead of an atomic variable and cmpxchg() (Alexei) v3: - Put lock and the members it protects into an embedded 'owner' struct (Daniel) Fixes: 3324b58 ("ebpf: misc core cleanup") Reported-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20211026110019.363464-1-toke@redhat.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 15dec6d commit dd2260e

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,11 @@ struct bpf_array_aux {
862862
* stored in the map to make sure that all callers and callees have
863863
* the same prog type and JITed flag.
864864
*/
865-
enum bpf_prog_type type;
866-
bool jited;
865+
struct {
866+
spinlock_t lock;
867+
enum bpf_prog_type type;
868+
bool jited;
869+
} owner;
867870
/* Programs with direct jumps into programs part of this array. */
868871
struct list_head poke_progs;
869872
struct bpf_map *map;

‎kernel/bpf/arraymap.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ static struct bpf_map *prog_array_map_alloc(union bpf_attr *attr)
10251025
INIT_WORK(&aux->work, prog_array_map_clear_deferred);
10261026
INIT_LIST_HEAD(&aux->poke_progs);
10271027
mutex_init(&aux->poke_mutex);
1028+
spin_lock_init(&aux->owner.lock);
10281029

10291030
map = array_map_alloc(attr);
10301031
if (IS_ERR(map)) {

‎kernel/bpf/core.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,20 +1775,26 @@ static unsigned int __bpf_prog_ret0_warn(const void *ctx,
17751775
bool bpf_prog_array_compatible(struct bpf_array *array,
17761776
const struct bpf_prog *fp)
17771777
{
1778+
bool ret;
1779+
17781780
if (fp->kprobe_override)
17791781
return false;
17801782

1781-
if (!array->aux->type) {
1783+
spin_lock(&array->aux->owner.lock);
1784+
1785+
if (!array->aux->owner.type) {
17821786
/* There's no owner yet where we could check for
17831787
* compatibility.
17841788
*/
1785-
array->aux->type = fp->type;
1786-
array->aux->jited = fp->jited;
1787-
return true;
1789+
array->aux->owner.type = fp->type;
1790+
array->aux->owner.jited = fp->jited;
1791+
ret = true;
1792+
} else {
1793+
ret = array->aux->owner.type == fp->type &&
1794+
array->aux->owner.jited == fp->jited;
17881795
}
1789-
1790-
return array->aux->type == fp->type &&
1791-
array->aux->jited == fp->jited;
1796+
spin_unlock(&array->aux->owner.lock);
1797+
return ret;
17921798
}
17931799

17941800
static int bpf_check_tail_call(const struct bpf_prog *fp)

‎kernel/bpf/syscall.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,10 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
535535

536536
if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
537537
array = container_of(map, struct bpf_array, map);
538-
type = array->aux->type;
539-
jited = array->aux->jited;
538+
spin_lock(&array->aux->owner.lock);
539+
type = array->aux->owner.type;
540+
jited = array->aux->owner.jited;
541+
spin_unlock(&array->aux->owner.lock);
540542
}
541543

542544
seq_printf(m,

0 commit comments

Comments
 (0)