Skip to content

Commit af58e7e

Browse files
tohojoborkmann
authored andcommitted
xdp: Fix race in dev_map_hash_update_elem() when replacing element
syzbot found a crash in dev_map_hash_update_elem(), when replacing an element with a new one. Jesper correctly identified the cause of the crash as a race condition between the initial lookup in the map (which is done before taking the lock), and the removal of the old element. Rather than just add a second lookup into the hashmap after taking the lock, fix this by reworking the function logic to take the lock before the initial lookup. Fixes: 6f9d451 ("xdp: Add devmap_hash map type for looking up devices by hashed index") Reported-and-tested-by: syzbot+4e7a85b1432052e8d6f8@syzkaller.appspotmail.com Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent a4fa6e1 commit af58e7e

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎kernel/bpf/devmap.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,19 +650,22 @@ static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
650650
u32 ifindex = *(u32 *)value;
651651
u32 idx = *(u32 *)key;
652652
unsigned long flags;
653+
int err = -EEXIST;
653654

654655
if (unlikely(map_flags > BPF_EXIST || !ifindex))
655656
return -EINVAL;
656657

658+
spin_lock_irqsave(&dtab->index_lock, flags);
659+
657660
old_dev = __dev_map_hash_lookup_elem(map, idx);
658661
if (old_dev && (map_flags & BPF_NOEXIST))
659-
return -EEXIST;
662+
goto out_err;
660663

661664
dev = __dev_map_alloc_node(net, dtab, ifindex, idx);
662-
if (IS_ERR(dev))
663-
return PTR_ERR(dev);
664-
665-
spin_lock_irqsave(&dtab->index_lock, flags);
665+
if (IS_ERR(dev)) {
666+
err = PTR_ERR(dev);
667+
goto out_err;
668+
}
666669

667670
if (old_dev) {
668671
hlist_del_rcu(&old_dev->index_hlist);
@@ -683,6 +686,10 @@ static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
683686
call_rcu(&old_dev->rcu, __dev_map_entry_free);
684687

685688
return 0;
689+
690+
out_err:
691+
spin_unlock_irqrestore(&dtab->index_lock, flags);
692+
return err;
686693
}
687694

688695
static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,

0 commit comments

Comments
 (0)