Skip to contentSkip to conversation

l2tp: Serialize access to sk_user_data with sock lock

PATCH Superseded Jakub Sitnicki 3 participants 8 messages Aug 15, 2022 – Aug 17, 2022

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 l2tp: Serialize access to sk_user_data with sock lock

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

v2:
- update Fixes to point to origin of the bug
- use real names in Reported/Tested-by tags

Fixes 3557baabf280 ("[L2TP]: PPP over L2TP driver core")

---
Cc: Tom Parkin <[email protected]>

 net/l2tp/l2tp_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/l2tp/l2tp_core.c +11 -6
index 7499c51b1850..9f5f86bfc395 100644
@@ -1469,16 +1469,18 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock = sockfd_lookup(tunnel->fd, &ret);
 		if (!sock)
 			goto err;
-
-		ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
-		if (ret < 0)
-			goto err_sock;
 	}
 
+	sk = sock->sk;
+	lock_sock(sk);
+
+	ret = l2tp_validate_socket(sk, net, tunnel->encap);
+	if (ret < 0)
+		goto err_sock;
+
 	tunnel->l2tp_net = net;
 	pn = l2tp_pernet(net);
 
-	sk = sock->sk;
 	sock_hold(sk);
 	tunnel->sock = sk;
 
@@ -1504,7 +1506,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 
 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
 	} else {
-		sk->sk_user_data = tunnel;
+		rcu_assign_sk_user_data(sk, tunnel);
 	}
 
 	tunnel->old_sk_destruct = sk->sk_destruct;
@@ -1518,6 +1520,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 	if (tunnel->fd >= 0)
 		sockfd_put(sock);
 
+	release_sock(sk);
 	return 0;
 
 err_sock:
@@ -1525,6 +1528,8 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock_release(sock);
 	else
 		sockfd_put(sock);
+
+	release_sock(sk);
 err:
 	return ret;
 }
Signature · 1 line
2.35.3
#1 lore raw
Quoted 7 lines · Jakub Sitnicki

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

Quoted 8 lines

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

v2:
- update Fixes to point to origin of the bug
- use real names in Reported/Tested-by tags

Fixes 3557baabf280 ("[L2TP]: PPP over L2TP driver core")

This still seems wrong to me.

In 3557baabf280 pppol2tp_connect checks/sets sk_user_data with lock_sock held.

Quoted 66 lines

---
Cc: Tom Parkin <[email protected]>

 net/l2tp/l2tp_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/l2tp/l2tp_core.c +11 -6
index 7499c51b1850..9f5f86bfc395 100644
@@ -1469,16 +1469,18 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock = sockfd_lookup(tunnel->fd, &ret);
 		if (!sock)
 			goto err;
-
-		ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
-		if (ret < 0)
-			goto err_sock;
 	}
 
+	sk = sock->sk;
+	lock_sock(sk);
+
+	ret = l2tp_validate_socket(sk, net, tunnel->encap);
+	if (ret < 0)
+		goto err_sock;
+
 	tunnel->l2tp_net = net;
 	pn = l2tp_pernet(net);
 
-	sk = sock->sk;
 	sock_hold(sk);
 	tunnel->sock = sk;
 
@@ -1504,7 +1506,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 
 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
 	} else {
-		sk->sk_user_data = tunnel;
+		rcu_assign_sk_user_data(sk, tunnel);
 	}
 
 	tunnel->old_sk_destruct = sk->sk_destruct;
@@ -1518,6 +1520,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 	if (tunnel->fd >= 0)
 		sockfd_put(sock);
 
+	release_sock(sk);
 	return 0;
 
 err_sock:
@@ -1525,6 +1528,8 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock_release(sock);
 	else
 		sockfd_put(sock);
+
+	release_sock(sk);
 err:
 	return ret;
 }
Signature · 1 line
2.35.3
Signature · 4 lines
Tom Parkin
Katalix Systems Ltd
https://katalix.com
Catalysts for your Embedded Linux software development
#2 lore raw
Quoted 16 lines · Tom Parkin

[[PGP Signed Part:Undecided]]

Quoted 14 lines · Jakub Sitnicki

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

v2:
- update Fixes to point to origin of the bug
- use real names in Reported/Tested-by tags

Quoted 6 lines
Quoted 1 line
Fixes 3557baabf280 ("[L2TP]: PPP over L2TP driver core")

This still seems wrong to me.

In 3557baabf280 pppol2tp_connect checks/sets sk_user_data with lock_sock held.

I think you are referring to the PPP-over-L2TP socket, not the UDP socket. In pppol2tp_prepare_tunnel_socket() @ 3557baabf280 we're not holding the sock lock over the UDP socket, AFAICT.

Quoted 67 lines
Quoted 67 lines

---
Cc: Tom Parkin <[email protected]>

 net/l2tp/l2tp_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/l2tp/l2tp_core.c +11 -6
index 7499c51b1850..9f5f86bfc395 100644
@@ -1469,16 +1469,18 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock = sockfd_lookup(tunnel->fd, &ret);
 		if (!sock)
 			goto err;
-
-		ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
-		if (ret < 0)
-			goto err_sock;
 	}
 
+	sk = sock->sk;
+	lock_sock(sk);
+
+	ret = l2tp_validate_socket(sk, net, tunnel->encap);
+	if (ret < 0)
+		goto err_sock;
+
 	tunnel->l2tp_net = net;
 	pn = l2tp_pernet(net);
 
-	sk = sock->sk;
 	sock_hold(sk);
 	tunnel->sock = sk;
 
@@ -1504,7 +1506,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 
 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
 	} else {
-		sk->sk_user_data = tunnel;
+		rcu_assign_sk_user_data(sk, tunnel);
 	}
 
 	tunnel->old_sk_destruct = sk->sk_destruct;
@@ -1518,6 +1520,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 	if (tunnel->fd >= 0)
 		sockfd_put(sock);
 
+	release_sock(sk);
 	return 0;
 
 err_sock:
@@ -1525,6 +1528,8 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock_release(sock);
 	else
 		sockfd_put(sock);
+
+	release_sock(sk);
 err:
 	return ret;
 }
Signature · 1 line
2.35.3
#3 lore raw
Quoted 19 lines · Jakub Sitnicki
Quoted 18 lines · Tom Parkin

[[PGP Signed Part:Undecided]]

Quoted 15 lines · Jakub Sitnicki

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

v2:
- update Fixes to point to origin of the bug
- use real names in Reported/Tested-by tags

Fixes 3557baabf280 ("[L2TP]: PPP over L2TP driver core")
Quoted 8 lines
Quoted 4 lines

This still seems wrong to me.

In 3557baabf280 pppol2tp_connect checks/sets sk_user_data with lock_sock held.

I think you are referring to the PPP-over-L2TP socket, not the UDP socket. In pppol2tp_prepare_tunnel_socket() @ 3557baabf280 we're not holding the sock lock over the UDP socket, AFAICT.

Yes, you're quite right -- my apologies.

Quoted 68 lines
Quoted 67 lines
Quoted 67 lines

---
Cc: Tom Parkin <[email protected]>

 net/l2tp/l2tp_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
net/l2tp/l2tp_core.c +11 -6
index 7499c51b1850..9f5f86bfc395 100644
@@ -1469,16 +1469,18 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock = sockfd_lookup(tunnel->fd, &ret);
 		if (!sock)
 			goto err;
-
-		ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
-		if (ret < 0)
-			goto err_sock;
 	}
 
+	sk = sock->sk;
+	lock_sock(sk);
+
+	ret = l2tp_validate_socket(sk, net, tunnel->encap);
+	if (ret < 0)
+		goto err_sock;
+
 	tunnel->l2tp_net = net;
 	pn = l2tp_pernet(net);
 
-	sk = sock->sk;
 	sock_hold(sk);
 	tunnel->sock = sk;
 
@@ -1504,7 +1506,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 
 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
 	} else {
-		sk->sk_user_data = tunnel;
+		rcu_assign_sk_user_data(sk, tunnel);
 	}
 
 	tunnel->old_sk_destruct = sk->sk_destruct;
@@ -1518,6 +1520,7 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 	if (tunnel->fd >= 0)
 		sockfd_put(sock);
 
+	release_sock(sk);
 	return 0;
 
 err_sock:
@@ -1525,6 +1528,8 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
 		sock_release(sock);
 	else
 		sockfd_put(sock);
+
+	release_sock(sk);
 err:
 	return ret;
 }
Signature · 1 line
2.35.3
maintainer
#4 lore raw
In reply to Jakub Sitnicki
Quoted 9 lines · On Mon, 15 Aug 2022 15:01:07 +0200 Jakub Sitnicki wrote:

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

Note to other netdev maintainers that based on the discussion about the reuseport locking it's unclear whether we shouldn't also take the callback lock...

#5 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 7 lines · On Mon, 15 Aug 2022 15:01:07 +0200 Jakub Sitnicki wrote:

sk->sk_user_data has multiple users, which are not compatible with each other. To synchronize the users, any check-if-unused-and-set access to the pointer has to happen with sock lock held.

l2tp currently fails to grab the lock when modifying the underlying tunnel socket. Fix it by adding appropriate locking.

Quoted 6 lines
Quoted 2 lines

We don't to grab the lock when l2tp clears sk_user_data, because it happens only in sk->sk_destruct, when the sock is going away.

Note to other netdev maintainers that based on the discussion about the reuseport locking it's unclear whether we shouldn't also take the callback lock...

You're right. reuseport_array, psock, and kcm protect sk_user_data with the callback lock, not the sock lock. Need to fix it.

maintainer
#6 lore raw
Quoted 6 lines · On Wed, 17 Aug 2022 16:33:33 +0200 Jakub Sitnicki wrote:
Quoted 3 lines

Note to other netdev maintainers that based on the discussion about the reuseport locking it's unclear whether we shouldn't also take the callback lock...

You're right. reuseport_array, psock, and kcm protect sk_user_data with the callback lock, not the sock lock. Need to fix it.

Where 'it' == current patch? Would you mind adding to the kdoc on sk_user_data that it's protected by the callback lock while at it?

#7 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 6 lines · On Wed, 17 Aug 2022 16:33:33 +0200 Jakub Sitnicki wrote:
Quoted 3 lines

Note to other netdev maintainers that based on the discussion about the reuseport locking it's unclear whether we shouldn't also take the callback lock...

You're right. reuseport_array, psock, and kcm protect sk_user_data with the callback lock, not the sock lock. Need to fix it.

Where 'it' == current patch? Would you mind adding to the kdoc on sk_user_data that it's protected by the callback lock while at it?

Yes, will prepare a v3 for review. Sorry, should have been explicit.

Will add a kdoc. Great idea.