From: Josef Bacik bpf_sk_storage_map_seq_find_next() hands out an element only if its local_storage pointer is set. __bpf_sk_storage_map_seq_show() then reads the pointer a second time and dereferences it without a test. Since bpf_local_storage_destroy() unlinks the elements with bpf_selem_unlink_nofail(), the pointer is reset to NULL when the socket which owns the element is destroyed, so it can change between the two reads. Reading the iterator while sockets with an element in the map are closed ends in a NULL pointer dereference. The sockets need not belong to the reader: where a program gives every new socket an element, an unprivileged user who opens and closes sockets is enough. Skip such an element, as the TCP and UDP iterators skip a socket which has been unhashed. Fixes: 0be08389c7f2 ("bpf: Switch to bpf_selem_unlink_nofail in bpf_local_storage_{map_free, destroy}") Signed-off-by: Josef Bacik Signed-off-by: Alexei Starovoitov --- net/core/bpf_sk_storage.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index b5349bec5695..aea67b446c7c 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -813,6 +813,9 @@ static int __bpf_sk_storage_map_seq_show(struct seq_file *seq, ctx.map = info->map; if (selem) { sk_storage = rcu_dereference(selem->local_storage); + /* reset since ->next() looked: the socket goes away */ + if (!sk_storage) + return SEQ_SKIP; ctx.sk = sk_storage->owner; ctx.value = SDATA(selem)->data; } -- 2.55.0