Skip to contentSkip to conversation

flow_dissector: Fix potential use-after-free on BPF_PROG_DETACH

PATCH Applied Jakub Sitnicki 3 participants 3 messages Aug 21, 2019 – Aug 23, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 flow_dissector: Fix potential use-after-free on BPF_PROG_DETACH

Call to bpf_prog_put(), with help of call_rcu(), queues an RCU-callback to free the program once a grace period has elapsed. The callback can run together with new RCU readers that started after the last grace period. New RCU readers can potentially see the "old" to-be-freed or already-freed pointer to the program object before the RCU update-side NULLs it.

Reorder the operations so that the RCU update-side resets the protected pointer before the end of the grace period after which the program will be freed.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")
---
 net/core/flow_dissector.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/flow_dissector.c +1 -1
index 3e6fedb57bc1..2470b4b404e6 100644
@@ -142,8 +142,8 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
 		mutex_unlock(&flow_dissector_mutex);
 		return -ENOENT;
 	}
-	bpf_prog_put(attached);
 	RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
+	bpf_prog_put(attached);
 	mutex_unlock(&flow_dissector_mutex);
 	return 0;
 }
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw

This makes sense, thanks!

Quoted 35 lines · Jakub Sitnicki

Call to bpf_prog_put(), with help of call_rcu(), queues an RCU-callback to free the program once a grace period has elapsed. The callback can run together with new RCU readers that started after the last grace period. New RCU readers can potentially see the "old" to-be-freed or already-freed pointer to the program object before the RCU update-side NULLs it.

Reorder the operations so that the RCU update-side resets the protected pointer before the end of the grace period after which the program will be freed.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")
---
 net/core/flow_dissector.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/flow_dissector.c +1 -2
index 3e6fedb57bc1..2470b4b404e6 100644
@@ -142,8 +142,8 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
                mutex_unlock(&flow_dissector_mutex);
                return -ENOENT;
        }
-       bpf_prog_put(attached);
        RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
+       bpf_prog_put(attached);
        mutex_unlock(&flow_dissector_mutex);
        return 0;
 }
--

2.20.1

maintainer Applied
#2 lore raw
In reply to Jakub Sitnicki
Quoted 6 lines · Jakub Sitnicki

Call to bpf_prog_put(), with help of call_rcu(), queues an RCU-callback to free the program once a grace period has elapsed. The callback can run together with new RCU readers that started after the last grace period. New RCU readers can potentially see the "old" to-be-freed or already-freed pointer to the program object before the RCU update-side NULLs it.

Quoted 7 lines

Reorder the operations so that the RCU update-side resets the protected pointer before the end of the grace period after which the program will be freed.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")

Applied, thanks!