bpf: syscall: a possible sleep-in-atomic-context bug in map_update_elem()
Subsystems (2)
The kernel may sleep with holding a rcu read lock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC] kmalloc(GFP_KERNEL)kernel/kthread.c, 283: kmalloc in __kthread_create_on_node kernel/kthread.c, 365: __kthread_create_on_node in kthread_create_on_node kernel/bpf/cpumap.c, 368: kthread_create_on_node in __cpu_map_entry_alloc kernel/bpf/cpumap.c, 490: __cpu_map_entry_alloc in cpu_map_update_elem kernel/bpf/syscall.c, 724: [FUNC_PTR]cpu_map_update_elem in map_update_elem kernel/bpf/syscall.c, 723: rcu_read_lock in map_update_elem
Note that [FUNC_PTR] means a function pointer call is used.
I do not find a good way to fix it, so I only report. This is found by my static analysis tool (DSAC).
Best wishes,
Jia-Ju Bai
Quoted 12 lines · Jia-Ju Bai
The kernel may sleep with holding a rcu read lock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kmalloc(GFP_KERNEL)kernel/kthread.c, 283: kmalloc in__kthread_create_on_nodekernel/kthread.c, 365:__kthread_create_on_nodeinkthread_create_on_nodekernel/bpf/cpumap.c, 368:kthread_create_on_nodein__cpu_map_entry_allockernel/bpf/cpumap.c, 490:__cpu_map_entry_allocincpu_map_update_elemkernel/bpf/syscall.c, 724: [FUNC_PTR]cpu_map_update_elem inmap_update_elemkernel/bpf/syscall.c, 723:rcu_read_lockinmap_update_elem
Quoted 4 lines
Note that [
FUNC_PTR] means a function pointer call is used.I do not find a good way to fix it, so I only report. This is found by my static analysis tool (DSAC).
Thanks for the report Jia-Ju! In the map_update_elem() from syscall path there's a check map->map_type == BPF_MAP_TYPE_CPUMAP, where we call the cpumap's map->ops->map_update_elem() while /not/ being under rcu_read_lock() as in other cases, so looks okay to me. Could you point out the case for being under rcu_read_lock() more specifically which the tool found?
Thanks,
Daniel
Quoted 15 lines · Daniel Borkmann
Quoted 14 lines · Jia-Ju Bai
The kernel may sleep with holding a rcu read lock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kmalloc(GFP_KERNEL)kernel/kthread.c, 283: kmalloc in__kthread_create_on_nodekernel/kthread.c, 365:__kthread_create_on_nodeinkthread_create_on_nodekernel/bpf/cpumap.c, 368:kthread_create_on_nodein__cpu_map_entry_allockernel/bpf/cpumap.c, 490:__cpu_map_entry_allocincpu_map_update_elemkernel/bpf/syscall.c, 724: [FUNC_PTR]cpu_map_update_elem inmap_update_elemkernel/bpf/syscall.c, 723:rcu_read_lockinmap_update_elemNote that [
FUNC_PTR] means a function pointer call is used.
Quoted 8 lines
Quoted 2 lines
I do not find a good way to fix it, so I only report. This is found by my static analysis tool (DSAC).
Thanks for the report Jia-Ju! In the
map_update_elem()from syscall path there's a checkmap->map_type==BPF_MAP_TYPE_CPUMAP, where we call the cpumap'smap->ops->map_update_elem()while /not/ being underrcu_read_lock()as in other cases, so looks okay to me. Could you point out the case for being underrcu_read_lock()more specifically which the tool found?
Thanks for your reply :)
My tool cannot accurately track the case of map->map_type at present...
According to my code review, there is a indeed check on line 697 in Linux-4.16:
else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
err = map->ops->map_update_elem(map, key, value, attr->flags);
goto out;
}
But there is a call to map->ops->map_update_elem() that is under rcu_read_lock on line 724:
rcu_read_lock();
err = map->ops->map_update_elem(map, key, value, attr->flags);
rcu_read_unlock();So I think if map->map_type is not equal to BPF_MAP_TYPE_CPUMAP, map->ops->map_update_elem() can still be called under rcu_read_lock, is it right?
Best wishes,
Jia-Ju Bai
Quoted 19 lines · Jia-Ju Bai
Quoted 16 lines · Daniel Borkmann
Quoted 14 lines · Jia-Ju Bai
The kernel may sleep with holding a rcu read lock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kmalloc(GFP_KERNEL)kernel/kthread.c, 283: kmalloc in__kthread_create_on_nodekernel/kthread.c, 365:__kthread_create_on_nodeinkthread_create_on_nodekernel/bpf/cpumap.c, 368:kthread_create_on_nodein__cpu_map_entry_allockernel/bpf/cpumap.c, 490:__cpu_map_entry_allocincpu_map_update_elemkernel/bpf/syscall.c, 724: [FUNC_PTR]cpu_map_update_elem inmap_update_elemkernel/bpf/syscall.c, 723:rcu_read_lockinmap_update_elem
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Note that [
FUNC_PTR] means a function pointer call is used.I do not find a good way to fix it, so I only report. This is found by my static analysis tool (DSAC).
Maybe your static analysis tool have false positives in this case?
Quoted 24 lines
Quoted 7 lines
Thanks for the report Jia-Ju! In the
map_update_elem()from syscall path there's a checkmap->map_type==BPF_MAP_TYPE_CPUMAP, where we call the cpumap'smap->ops->map_update_elem()while /not/ being underrcu_read_lock()as in other cases, so looks okay to me. Could you point out the case for being underrcu_read_lock()more specifically which the tool found?Thanks for your reply :)
My tool cannot accurately track the case ofmap->map_typeat present...According to my code review, there is a indeed check on line 697 in Linux-4.16:
else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { err = map->ops->map_update_elem(map, key, value, attr->flags); goto out; } But there is a call to map->ops->map_update_elem() that is under rcu_read_lock on line 724: rcu_read_lock(); err = map->ops->map_update_elem(map, key, value, attr->flags); rcu_read_unlock();
Quoted 3 lines
So I think if
map->map_typeis not equal toBPF_MAP_TYPE_CPUMAP,map->ops->map_update_elem()can still be called underrcu_read_lock, is it right?
map->ops->map_update_elem() can be called under rcu_read_lock(), but since it is not type cpumap, the function should not be cpu_map_update_elem(). Could you double check your static analysis tool?
Quoted 4 lines
Best wishes,
Jia-Ju Bai
Quoted 45 lines · Y Song
Quoted 16 lines · Jia-Ju Bai
Quoted 14 lines · Daniel Borkmann
Quoted 13 lines · Jia-Ju Bai
The kernel may sleep with holding a rcu read lock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kmalloc(GFP_KERNEL)kernel/kthread.c, 283: kmalloc in__kthread_create_on_nodekernel/kthread.c, 365:__kthread_create_on_nodeinkthread_create_on_nodekernel/bpf/cpumap.c, 368:kthread_create_on_nodein__cpu_map_entry_allockernel/bpf/cpumap.c, 490:__cpu_map_entry_allocincpu_map_update_elemkernel/bpf/syscall.c, 724: [FUNC_PTR]cpu_map_update_elem inmap_update_elemkernel/bpf/syscall.c, 723:rcu_read_lockinmap_update_elemQuoted 4 lines
Quoted 4 lines
Quoted 4 lines
Note that [
FUNC_PTR] means a function pointer call is used.I do not find a good way to fix it, so I only report. This is found by my static analysis tool (DSAC).
Maybe your static analysis tool have false positives in this case?
Quoted 22 lines
Quoted 6 lines
Thanks for the report Jia-Ju! In the
map_update_elem()from syscall path there's a checkmap->map_type==BPF_MAP_TYPE_CPUMAP, where we call the cpumap'smap->ops->map_update_elem()while /not/ being underrcu_read_lock()as in other cases, so looks okay to me. Could you point out the case for being underrcu_read_lock()more specifically which the tool found?Thanks for your reply :)
My tool cannot accurately track the case ofmap->map_typeat present...According to my code review, there is a indeed check on line 697 in Linux-4.16:
else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { err = map->ops->map_update_elem(map, key, value, attr->flags); goto out; } But there is a call to map->ops->map_update_elem() that is under rcu_read_lock on line 724: rcu_read_lock(); err = map->ops->map_update_elem(map, key, value, attr->flags); rcu_read_unlock();
Quoted 6 lines
Quoted 3 lines
So I think if
map->map_typeis not equal toBPF_MAP_TYPE_CPUMAP,map->ops->map_update_elem()can still be called underrcu_read_lock, is it right?
map->ops->map_update_elem()can be called underrcu_read_lock(), but since it is not type cpumap, the function should not becpu_map_update_elem(). Could you double check your static analysis tool?
Thanks for your reply :)
I have checked the code again, and find that my report is not correct here. It is because that my tool does not handle the context of function pointer assignment.
Best wishes,
Jia-Ju Bai