Skip to content

Commit f3ac55c

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf, sockmap: avoid using sk_socket after free when reading
There are potential concurrency issues, as shown below. ''' CPU0 CPU1 sk_psock_verdict_data_ready: socket *sock = sk->sk_socket if (!sock) return close(fd): ... ops->release() if (!sock->ops) return sock->ops = NULL rcu_call(sock) free(sock) READ_ONCE(sock->ops) ^ use 'sock' after free ''' RCU is not applicable to Unix sockets read path, because the Unix socket implementation itself assumes it's always in process context and heavily uses mutex_lock, so, we can't call read_skb within rcu lock. Incrementing the psock reference count would not help either, since sock_map_close() does not wait for data_ready() to complete its execution. While we don't utilize sk_socket here, implementing read_skb at the sock layer instead of socket layer might be architecturally preferable ? However, deferring this optimization as current fix adequately addresses the immediate issue. Fixes: c638291 ("af_unix: Implement ->psock_update_sk_prot()") Reported-by: [email protected] Closes: https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Jiayuan Chen <[email protected]>
1 parent e582cbd commit f3ac55c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

net/core/skmsg.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -1231,17 +1231,24 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb)
12311231

12321232
static void sk_psock_verdict_data_ready(struct sock *sk)
12331233
{
1234-
struct socket *sock = sk->sk_socket;
1234+
struct socket *sock;
12351235
const struct proto_ops *ops;
12361236
int copied;
12371237

12381238
trace_sk_data_ready(sk);
12391239

1240-
if (unlikely(!sock))
1240+
rcu_read_lock();
1241+
sock = sk->sk_socket;
1242+
if (unlikely(!sock)) {
1243+
rcu_read_unlock();
12411244
return;
1245+
}
12421246
ops = READ_ONCE(sock->ops);
1243-
if (!ops || !ops->read_skb)
1247+
if (!ops || !ops->read_skb) {
1248+
rcu_read_unlock();
12441249
return;
1250+
}
1251+
rcu_read_unlock();
12451252
copied = ops->read_skb(sk, sk_psock_verdict_recv);
12461253
if (copied >= 0) {
12471254
struct sk_psock *psock;

0 commit comments

Comments
 (0)