A sockops prog can add a child that is not accepted yet to a sockmap at
BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB. An sk_msg redirect to this child,
ingress or egress, takes lock_sock() on it. If the listener is closed at
that time, inet_csk_listen_stop() hits:
WARN_ON(sock_owned_by_user(child));
WARNING: net/ipv4/inet_connection_sock.c:1492 at inet_csk_listen_stop+0x7ba/0xc40
Call Trace:
__tcp_close+0xb64/0xee0
tcp_close+0x24/0xf0
inet_release+0x10a/0x240
__sock_release+0xaf/0x270
sock_close+0x18/0x30
__fput+0x360/0xa90
fput_close_sync+0xe6/0x1d0
__x64_sys_close+0x80/0xe0
x64_sys_call+0x2680/0x27e0
do_syscall_64+0xe5/0x610
entry_SYSCALL_64_after_hwframe+0x76/0x7e
Commit 4aeaad1c536e ("bpf, sockmap: Check the target of an egress
redirect before each send") already refuses an egress redirect to such a
child, but only after lock_sock(). Do it before taking the lock: a child
has no sk_socket until accept() has taken it off the queue.
An ingress redirect to such a child works today and the data is read
after accept(), so it can't be refused. It only queues the data and
never sleeps, so take the lock of a child with lock_sock_fast() instead.
The child is not owned then and inet_csk_listen_stop() waits on
bh_lock_sock(). An accepted socket keeps lock_sock(). Under
lock_sock_fast() its memcg charge would be atomic and forced at the
limit. A child has no memcg until accept().
sock_map_free() and sock_hash_free() also lock the sockets in the map.
They can't refuse, so unlink such a child without the lock, as a delete
from the map already does.
A child with a strparser is still locked by strp_done() and the
strparser work. That is in the strparser code and is left for later.
Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Fixes: 7e81a3530206 ("bpf: Sockmap, ensure sock lock held during tear down")
Signed-off-by: Jiayuan Chen
---
net/core/sock_map.c | 27 +++++++++++++++++++++++----
net/ipv4/tcp_bpf.c | 30 +++++++++++++++++++++++++++---
2 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 282c6a4b2b05f..b86b37dd26e9b 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -350,6 +350,19 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
return ret;
}
+/*
+ * The listener frees a child that nobody has accepted yet without waiting
+ * for the owner of its socket lock. Unlink such a child without the lock,
+ * as a delete does.
+ */
+static bool sock_map_free_lock_sk(struct sock *sk)
+{
+ if (!READ_ONCE(sk->sk_socket))
+ return false;
+ lock_sock(sk);
+ return true;
+}
+
static void sock_map_free(struct bpf_map *map)
{
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
@@ -366,12 +379,15 @@ static void sock_map_free(struct bpf_map *map)
sk = xchg(psk, NULL);
if (sk) {
+ bool locked;
+
sock_hold(sk);
- lock_sock(sk);
+ locked = sock_map_free_lock_sk(sk);
rcu_read_lock();
sock_map_unref(sk, psk);
rcu_read_unlock();
- release_sock(sk);
+ if (locked)
+ release_sock(sk);
sock_put(sk);
}
}
@@ -1191,12 +1207,15 @@ static void sock_hash_free(struct bpf_map *map)
* link to sockhash.
*/
hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
+ bool locked;
+
hlist_del(&elem->node);
- lock_sock(elem->sk);
+ locked = sock_map_free_lock_sk(elem->sk);
rcu_read_lock();
sock_map_unref(elem->sk, elem);
rcu_read_unlock();
- release_sock(elem->sk);
+ if (locked)
+ release_sock(elem->sk);
sock_put(elem->sk);
sock_hash_free_elem(htab, elem);
}
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 37d90ed5d8298..5b4444ee6d881 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -30,6 +30,21 @@ void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
__tcp_cleanup_rbuf(sk, skb->len);
}
+/*
+ * A child that nobody has accepted yet is freed by the listener under
+ * bh_lock_sock(), which doesn't expect an owner.
+ */
+static bool bpf_tcp_ingress_lock(struct sock *sk)
+ __acquires(&sk->sk_lock.slock)
+{
+ if (!READ_ONCE(sk->sk_socket))
+ return lock_sock_fast(sk);
+
+ lock_sock(sk);
+ __acquire(&sk->sk_lock.slock);
+ return true;
+}
+
static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
struct sk_msg *msg, u32 apply_bytes)
{
@@ -38,6 +53,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
u32 size, copied = 0;
struct sk_msg *tmp;
int i, ret = 0;
+ bool slow;
if (!msg->sg.size)
return 0;
@@ -46,7 +62,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
if (unlikely(!tmp))
return -ENOMEM;
- lock_sock(sk);
+ slow = bpf_tcp_ingress_lock(sk);
tmp->sg.start = msg->sg.start;
i = msg->sg.start;
do {
@@ -87,7 +103,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
kfree(tmp);
}
- release_sock(sk);
+ unlock_sock_fast(sk, slow);
return ret;
}
@@ -188,9 +204,17 @@ static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
struct sk_msg *msg, u32 bytes, int flags)
{
- struct sk_psock *psock = sk_psock_get(sk);
+ struct sk_psock *psock;
int ret;
+ /*
+ * The listener frees a child that nobody has accepted yet without
+ * waiting for the owner of its socket lock. Ingress doesn't own it.
+ */
+ if (!ingress && !READ_ONCE(sk->sk_socket))
+ return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
+
+ psock = sk_psock_get(sk);
if (unlikely(!psock))
return -EPIPE;
--
2.43.0