I have an explanation of why this error occurs, but I need an advice
on how to fix it.
Then NULL pointer deference occurs in the following function from offload.c:
int bpf_prog_offload_verifier_prep(struct bpf_prog *prog)
{
struct bpf_prog_offload *offload;
int ret = -ENODEV;
down_read(&bpf_devs_lock);
offload = prog->aux->offload;
if (offload) {
ret = offload->offdev->ops->prepare(prog);
^^^^^^
this pointer is NULL
offload->dev_state = !ret;
}
up_read(&bpf_devs_lock);
return ret;
}
# Short explanation
(a) call chain bpf_prog_load -> bpf_prog_dev_bound_init -> __bpf_prog_dev_bound_init
-> __bpf_offload_dev_netdev_register
might insert an instance of struct bpf_offload_netdev with {.offdev == NULL}
into hash table offload.c:offdevs;
(b) call chain bpf_prog_load -> bpf_check -> bpf_prog_offload_verifier_prep
assumes that from (prog->aux->offload != NULL)
follows (prog->aux->offload->offdev != NULL)
which is not the case because of (a).
# Long explanation
The reproducer generated by testbot has the following structure:
- in a loop call function execute_one(), which does the following
system calls in sequence:
- socket(AF_INET6, SOCK_RAW, IPPROTO_IGMP) = <some fd>
- ioctl(3, SIOCGIFINDEX, {ifr_name="batadv_slave_1"}) = 0
- bpf(BPF_PROG_LOAD,
{prog_type=BPF_PROG_TYPE_XDP, ... prog_flags=0x40, prog_ifindex=29, ...}) = -1 EINVAL
(referred to as program #1 below)
- socket(AF_INET6, SOCK_RAW, IPPROTO_IGMP) = <some fd>
- ioctl(4, SIOCGIFINDEX, {ifr_name="batadv_slave_1"}) = 0
- bpf(BPF_PROG_LOAD,
{prog_type=BPF_PROG_TYPE_XDP, ... prog_flags=0, ... prog_ifindex=29}) = -1 EINVAL
(referred to as program #2 below)
The error occurs when second bpf call is processed.
Interestingly, if sleep(1) is inserted somewhere between first and
second bpf calls error does not occur:
@@ -1246,6 +1246,7 @@ void execute_one(void)
*(uint32_t*)0x200009cc = 4;
syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0x20000940ul, /*size=*/0x90ul);
res = syscall(__NR_socket, /*domain=*/0xaul, /*type=*/3ul, /*proto=*/2);
+ // sleep(1); /* uncomment to hide the error */
if (res != -1)
r[2] = res;
memcpy((void*)0x20000100, "batadv_slave_1\000\000", 16);
## Control flow when error occurs
For program #1:
- bpf_prog_load():
- bpf_prog_is_dev_bound(prog->aux) is true
- bpf_prog_dev_bound_init
- prog->aux->offload_requested is 0 (because of 0x40 prog_flags)
- __bpf_prog_dev_bound_init
- netdev is "batadv_slave_1"
- bpf_offload_find_netdev(offload->netdev) == NULL,
(this is a lookup in hash table offload.c:offdevs)
which triggers a call to __bpf_offload_dev_netdev_register
- __bpf_offload_dev_netdev_register(NULL, offload->netdev)
registers struct bpf_offload_netdev with {.offdev = NULL}
for netdev "batadv_slave_1" in offload.c:offdevs hash table.
For program #2:
- bpf_prog_load():
- bpf_prog_is_dev_bound(prog->aux) is true
- bpf_prog_dev_bound_init
- prog->aux->offload_requested is 1 (because of 0x0 prog_flags)
- __bpf_prog_dev_bound_init
- netdev is "batadv_slave_1"
- bpf_offload_find_netdev(offload->netdev) != NULL,
this is struct bpf_offload_netdev with {.offdev = NULL}
created for program #1
- prog->aux->offload = struct bpf_prog_offload {.offload -> {.offdev = NULL}},
The bpf_prog_offload remembered for prog points to bpf_offload_netdev
with .offdev == NULL.
- ...
- bpf_check
- bpf_prog_offload_verifier_prep
- prog->aux->offload != NULL, but prog->aux->offload->offdev == NULL
=> null pointer deference.
## Control flow when error does not occur
For program #1:
- ... all as in the previous case ...
Some worker thread:
- kernel/bpf/core.c:bpf_prog_free_deferred, registered for program #1:
- bpf_prog_is_dev_bound(aux) is true
- bpf_prog_dev_bound_destroy
- netdev is "batadv_slave_1"
- (!ondev->offdev && list_empty(&ondev->progs)) is true
- __bpf_offload_dev_netdev_unregister
this removes struct bpf_offload_netdev with {.offdev = NULL}
from offload.c:offdevs hash table.
For program #2:
- bpf_prog_load():
- bpf_prog_is_dev_bound(prog->aux) is true
- bpf_prog_dev_bound_init
- prog->aux->offload_requested is 1 (because of 0x0 prog_flags)
- __bpf_prog_dev_bound_init
- netdev is "batadv_slave_1"
- bpf_offload_find_netdev(offload->netdev) == NULL
- bpf_prog_is_offloaded(prog->aux) is true
- -EINVAL is returned.