Skip to contentSkip to conversation

tcp: Fix out-of-bounds access for twsk in tcp_ao_established_key().

PATCH Superseded Kuniyuki Iwashima 2 participants 3 messages May 6 – May 7

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kuniyuki Iwashima

author reviewer
#0 lore raw

PATCH 1/1 tcp: Fix out-of-bounds access for twsk in tcp_ao_established_key().

lockdep_sock_is_held() was added in tcp_ao_established_key() by the cited commit.

It can be called from tcp_v[46]_timewait_ack() with twsk.

Since it does not have sk->sk_lock, the lockdep annotation results in out-of-bound access.

  $ pahole -C tcp_timewait_sock vmlinux | grep size
  	/* size: 288, cachelines: 5, members: 8 */
  $ pahole -C sock vmlinux | grep sk_lock
  	socket_lock_t              sk_lock;              /*   440   192 */

Let's not use lockdep_sock_is_held() for TCP_TIME_WAIT.

Fixes 6b2d11e2d8fc ("net/tcp: Add missing lockdep annotations for TCP-AO hlist traversals")
---
 net/ipv4/tcp_ao.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv4/tcp_ao.c +3 -1
index a97cdf3e6af4..e2720233e36b 100644
@@ -116,7 +116,9 @@ struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
 {
 	struct tcp_ao_key *key;
 
-	hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
+	hlist_for_each_entry_rcu(key, &ao->head, node,
+				 sk->sk_state == TCP_TIME_WAIT ||
+				 lockdep_sock_is_held(sk)) {
 		if ((sndid >= 0 && key->sndid != sndid) ||
 		    (rcvid >= 0 && key->rcvid != rcvid))
 			continue;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw
Quoted 28 lines · Kuniyuki Iwashima

lockdep_sock_is_held() was added in tcp_ao_established_key() by the cited commit.

It can be called from tcp_v[46]_timewait_ack() with twsk.

Since it does not have sk->sk_lock, the lockdep annotation results in out-of-bound access.

  $ pahole -C tcp_timewait_sock vmlinux | grep size
        /* size: 288, cachelines: 5, members: 8 */
  $ pahole -C sock vmlinux | grep sk_lock
        socket_lock_t              sk_lock;              /*   440   192 */

Let's not use lockdep_sock_is_held() for TCP_TIME_WAIT.

Fixes 6b2d11e2d8fc ("net/tcp: Add missing lockdep annotations for TCP-AO hlist traversals")
---
 net/ipv4/tcp_ao.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv4/tcp_ao.c +0 -0
index a97cdf3e6af4..e2720233e36b 100644

net/ipv4/tcp_ao.c struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk, · line 124

Quoted 10 lines
net/ipv4/tcp_ao.c +3 -1
@@ -116,7 +116,9 @@ struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
 {
        struct tcp_ao_key *key;
 
-       hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
+       hlist_for_each_entry_rcu(key, &ao->head, node,
+                                sk->sk_state == TCP_TIME_WAIT ||
+                                lockdep_sock_is_held(sk)) {
                if ((sndid >= 0 && key->sndid != sndid) ||
                    (rcvid >= 0 && key->rcvid != rcvid))
                        continue;

I wonder if a better fix would be to change __list_check_rcu() evaluation order.

Otherwise, this would mean that a TIME_WAIT socket would evade RCU LOCKDEP checks.

If a fix in tcp_ao.c is te way to go, I would suggest the opposite of what you did.

net/ipv4/tcp_ao.c +0 -0
index a97cdf3e6af4cf1ee1cd8c6361944536056543e6..0a4b38b315fed40899901ea5f66fbdffd776df38

100644

net/ipv4/tcp_ao.c +0 -0
@@ -116,7 +116,8 @@ struct tcp_ao_key *tcp_ao_established_key(const

struct sock *sk,

 {
        struct tcp_ao_key *key;

- hlist_for_each_entry_rcu(key, &ao->head, node,
lockdep_sock_is_held(sk)) {
+ hlist_for_each_entry_rcu(key, &ao->head, node,

+                                sk_fullsock(sk) && lockdep_sock_is_held(sk)) {
                if ((sndid >= 0 && key->sndid != sndid) ||
                    (rcvid >= 0 && key->rcvid != rcvid))
                        continue;
author reviewer
#2 lore raw
Quoted 43 lines · Eric Dumazet
Quoted 28 lines · Kuniyuki Iwashima

lockdep_sock_is_held() was added in tcp_ao_established_key() by the cited commit.

It can be called from tcp_v[46]_timewait_ack() with twsk.

Since it does not have sk->sk_lock, the lockdep annotation results in out-of-bound access.

  $ pahole -C tcp_timewait_sock vmlinux | grep size
        /* size: 288, cachelines: 5, members: 8 */
  $ pahole -C sock vmlinux | grep sk_lock
        socket_lock_t              sk_lock;              /*   440   192 */

Let's not use lockdep_sock_is_held() for TCP_TIME_WAIT.

Fixes 6b2d11e2d8fc ("net/tcp: Add missing lockdep annotations for TCP-AO hlist traversals")
---
 net/ipv4/tcp_ao.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv4/tcp_ao.c +0 -0
index a97cdf3e6af4..e2720233e36b 100644

net/ipv4/tcp_ao.c struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk, · line 124

Quoted 10 lines
net/ipv4/tcp_ao.c +3 -1
@@ -116,7 +116,9 @@ struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
 {
        struct tcp_ao_key *key;
 
-       hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
+       hlist_for_each_entry_rcu(key, &ao->head, node,
+                                sk->sk_state == TCP_TIME_WAIT ||
+                                lockdep_sock_is_held(sk)) {
                if ((sndid >= 0 && key->sndid != sndid) ||
                    (rcvid >= 0 && key->rcvid != rcvid))
                        continue;

I wonder if a better fix would be to change __list_check_rcu() evaluation order.

Quoted 2 lines

Otherwise, this would mean that a TIME_WAIT socket would evade RCU LOCKDEP checks.

Ah exactly,

Quoted 3 lines

If a fix in tcp_ao.c is te way to go, I would suggest the opposite of what you did.

will change that way.

Thanks !

Quoted 18 lines
net/ipv4/tcp_ao.c +0 -0
index a97cdf3e6af4cf1ee1cd8c6361944536056543e6..0a4b38b315fed40899901ea5f66fbdffd776df38

100644

net/ipv4/tcp_ao.c +2 -1
@@ -116,7 +116,8 @@ struct tcp_ao_key *tcp_ao_established_key(const
 struct sock *sk,
 {
        struct tcp_ao_key *key;
 
-       hlist_for_each_entry_rcu(key, &ao->head, node,
 lockdep_sock_is_held(sk)) {
+       hlist_for_each_entry_rcu(key, &ao->head, node,
+                                sk_fullsock(sk) && lockdep_sock_is_held(sk)) {
                if ((sndid >= 0 && key->sndid != sndid) ||
                    (rcvid >= 0 && key->rcvid != rcvid))
                        continue;