Skip to contentSkip to conversation

ovpn: fix use after free in unlock_ovpn()

RFC In review Marco Baffo 2 participants 2 messages May 27 – Jun 8

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

RFC 1/1 ovpn: fix use after free in unlock_ovpn()

unlock_ovpn() iterates over the release_list using llist_for_each_entry() and drops the peer reference inside the loop body via ovpn_peer_put().

If this drops the last reference, the peer is eventually freed. However, llist_for_each_entry() reads peer->release_entry.next in the loop advance expression, which runs after the body. By that time the peer may have already been freed, resulting in a use after free when advancing to the next list entry.

Fix this by using llist_for_each_entry_safe(), which caches the next pointer before executing the loop body.

---
 drivers/net/ovpn/peer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ovpn/peer.c +2 -2
index c02dfab51a6e..ff7c6ce9fcad 100644
@@ -26,11 +26,11 @@ static void unlock_ovpn(struct ovpn_priv *ovpn,
 			 struct llist_head *release_list)
 	__releases(&ovpn->lock)
 {
-	struct ovpn_peer *peer;
+	struct ovpn_peer *peer, *next;
 
 	spin_unlock_bh(&ovpn->lock);
 
-	llist_for_each_entry(peer, release_list->first, release_entry) {
+	llist_for_each_entry_safe(peer, next, release_list->first, release_entry) {
 		ovpn_socket_release(peer);
 		ovpn_peer_put(peer);
 	}
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

Hi Marco,

Quoted 9 lines · Marco Baffo

unlock_ovpn() iterates over the release_list using llist_for_each_entry() and drops the peer reference inside the loop body via ovpn_peer_put().

If this drops the last reference, the peer is eventually freed. However, llist_for_each_entry() reads peer->release_entry.next in the loop advance expression, which runs after the body. By that time the peer may have already been freed, resulting in a use after free when advancing to the next list entry.

Quoted 4 lines

Fix this by using llist_for_each_entry_safe(), which caches the next pointer before executing the loop body.

Nice fix. Please send this as PATCH as well.

Regards,

Quoted 22 lines
---
  drivers/net/ovpn/peer.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ovpn/peer.c +2 -2
index c02dfab51a6e..ff7c6ce9fcad 100644
@@ -26,11 +26,11 @@ static void unlock_ovpn(struct ovpn_priv *ovpn,
  			 struct llist_head *release_list)
  	__releases(&ovpn->lock)
  {
-	struct ovpn_peer *peer;
+	struct ovpn_peer *peer, *next;
 
  	spin_unlock_bh(&ovpn->lock);
 
-	llist_for_each_entry(peer, release_list->first, release_entry) {
+	llist_for_each_entry_safe(peer, next, release_list->first, release_entry) {
  		ovpn_socket_release(peer);
  		ovpn_peer_put(peer);
  	}
Signature · 8 lines
Antonio Quartulli



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel