From: Nguyen Ngoc Thang x25_kill_by_neigh() is reached from the receive path when the link layer goes away or the peer restarts: net_rx_action() lapbeth_napi_poll() x25_lapb_receive_frame() x25_link_terminated() / x25_link_control() x25_kill_by_neigh() lock_sock() lock_sock() may sleep, which is not allowed in softirq context: BUG: sleeping function called from invalid context at net/core/sock.c:3832 in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 29, name: ktimers/1 RCU nest depth: 2, expected: 0 ... lock_sock_nested+0x56/0x130 net/core/sock.c:3832 x25_kill_by_neigh+0x134/0x2a0 net/x25/af_x25.c:1778 x25_lapb_receive_frame+0x1b0/0xfb0 net/x25/x25_dev.c:138 Without PREEMPT_RT, lockdep reports an inconsistent lock state on sk_lock-AF_X25 instead. syzbot found this, and it is easy to trigger: "ip link set lapb0 down" on a lapbether device with a connected peer makes LAPB send a DISC, and the peer clears its calls from softirq. Take the socket spinlock instead, the same way the x25 timers and receive path already do. If the socket is not owned by user, disconnect it right away. If it is, mark it with X25_KILL_FLAG and let a new release_cb do the disconnect when the owner releases the socket. This keeps the serialization against x25_sendmsg()/x25_recvmsg()/x25_connect() that commit 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect") added lock_sock() for. Sockets that already have the flag set are skipped, so the rescan loop always terminates. NETDEV_DOWN runs in process context and the device may be freed right after, so keep waiting for socket owners there as before: a sleeping sweep makes sure no socket still uses the neighbour on return. Until its owner releases it, a flagged socket still holds the LCI of a call that no longer exists, while x25_link_control() may already have confirmed the restart. The LCI can then be reused, by the peer or locally through x25_new_lci(). Skip flagged sockets in __x25_find_socket(), so that frames for a reused LCI no longer end up in the old socket, and clear the flag only after x25_disconnect() has reset the LCI. Keep flagged sockets from transmitting in x25_kick() and x25_write_internal(). As the flag is set under the socket spinlock, take it around transmissions in process context and around the backlog processing too; otherwise a transmission that was already under way could still send frames of the old call after the restart has been confirmed. With that, the deferred x25_disconnect() only changes local state, and the old call is gone on the wire before the restart is answered, as it was before. x25_connect() accepts a listening socket, so a listener can carry the flag. Clear it in x25_make_new(), which copies the listener's flags. Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect") Reported-by: syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=9faa82ae2e94c5c7d2ef Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6 Signed-off-by: Nguyen Ngoc Thang [ms: hide flagged sockets and keep them silent, add the current report] Signed-off-by: Martin Schiller --- include/net/x25.h | 1 + net/x25/af_x25.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++------ net/x25/x25_in.c | 7 +++- net/x25/x25_out.c | 3 +- net/x25/x25_subr.c | 4 ++ 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/include/net/x25.h b/include/net/x25.h index 414f3fd99345455dd30f0cc827736fbce61fd99a..ed8ba01fa3cb396f80344a23f4de8d23d5e4a010 100644 --- a/include/net/x25.h +++ b/include/net/x25.h @@ -118,6 +118,7 @@ enum { #define X25_Q_BIT_FLAG 0 #define X25_INTERRUPT_FLAG 1 #define X25_ACCPT_APPRV_FLAG 2 +#define X25_KILL_FLAG 3 /** * struct x25_route - x25 routing entry diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c index 802688d5ad5687850764d0b779c72f039097b3d5..43c606a6bfa5a3cf69c905f230edf2c37a85e25d 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -200,6 +200,32 @@ static void x25_remove_socket(struct sock *sk) write_unlock_bh(&x25_list_lock); } +/* + * Process context only: wait for owners that x25_kill_by_neigh() deferred, + * so no socket still uses nb once the device goes away. + */ +static void x25_kill_by_neigh_sync(struct x25_neigh *nb) +{ + struct sock *s; + +again: + write_lock_bh(&x25_list_lock); + + sk_for_each(s, &x25_list) { + if (x25_sk(s)->neighbour == nb) { + sock_hold(s); + write_unlock_bh(&x25_list_lock); + lock_sock(s); + if (x25_sk(s)->neighbour == nb) + x25_disconnect(s, ENETUNREACH, 0, 0); + release_sock(s); + sock_put(s); + goto again; + } + } + write_unlock_bh(&x25_list_lock); +} + /* * Handle device status changes. */ @@ -222,6 +248,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event, nb = x25_get_neigh(dev); if (nb) { x25_link_terminated(nb); + x25_kill_by_neigh_sync(nb); x25_neigh_put(nb); } x25_route_device_down(dev); @@ -305,13 +332,16 @@ static struct sock *x25_find_listener(struct x25_address *addr, /* * Find a connected X.25 socket given my LCI and neighbour. + * Sockets flagged by x25_kill_by_neigh() are skipped: their call no + * longer exists, and the LCI may already belong to a new one. */ static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb) { struct sock *s; sk_for_each(s, &x25_list) - if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) { + if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb && + !test_bit(X25_KILL_FLAG, &x25_sk(s)->flags)) { sock_hold(s); goto found; } @@ -331,7 +361,8 @@ struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb) } /* - * Find a unique LCI for a given device. + * Find a unique LCI for a given device. The LCI of a socket flagged + * by x25_kill_by_neigh() counts as free, its call has already ended. */ static unsigned int x25_new_lci(struct x25_neigh *nb) { @@ -492,10 +523,25 @@ static int x25_listen(struct socket *sock, int backlog) return rc; } +/* Finish a link teardown that hit while the socket was owned by user. */ +static void x25_release_cb(struct sock *sk) +{ + struct x25_sock *x25 = x25_sk(sk); + + if (!test_bit(X25_KILL_FLAG, &x25->flags)) + return; + + /* Stay hidden from __x25_find_socket() until the LCI is reset */ + if (x25->neighbour) + x25_disconnect(sk, ENETUNREACH, 0, 0); + clear_bit(X25_KILL_FLAG, &x25->flags); +} + static struct proto x25_proto = { .name = "X25", .owner = THIS_MODULE, .obj_size = sizeof(struct x25_sock), + .release_cb = x25_release_cb, }; static struct sock *x25_alloc_socket(struct net *net, int kern) @@ -611,11 +657,39 @@ static struct sock *x25_make_new(struct sock *osk) x25->cudmatchlength = ox25->cudmatchlength; clear_bit(X25_INTERRUPT_FLAG, &x25->flags); + clear_bit(X25_KILL_FLAG, &x25->flags); x25_init_timers(sk); out: return sk; } +/* + * x25_kill_sock() flags a socket that is owned by user while holding the + * socket spinlock. Transmissions in process context take it as well, so + * that a flagged socket sends nothing more, not even from a transmission + * that was already under way when the flag was set. + */ +static void x25_write_internal_owned(struct sock *sk, int frametype) +{ + spin_lock_bh(&sk->sk_lock.slock); + x25_write_internal(sk, frametype); + spin_unlock_bh(&sk->sk_lock.slock); +} + +static void x25_kick_owned(struct sock *sk) +{ + spin_lock_bh(&sk->sk_lock.slock); + x25_kick(sk); + spin_unlock_bh(&sk->sk_lock.slock); +} + +static void x25_check_rbuf_owned(struct sock *sk) +{ + spin_lock_bh(&sk->sk_lock.slock); + x25_check_rbuf(sk); + spin_unlock_bh(&sk->sk_lock.slock); +} + static int x25_release(struct socket *sock) { struct sock *sk = sock->sk; @@ -640,7 +714,7 @@ static int x25_release(struct socket *sock) case X25_STATE_3: case X25_STATE_4: x25_clear_queues(sk); - x25_write_internal(sk, X25_CLEAR_REQUEST); + x25_write_internal_owned(sk, X25_CLEAR_REQUEST); x25_start_t23timer(sk); x25->state = X25_STATE_2; sk->sk_state = TCP_CLOSE; @@ -651,7 +725,7 @@ static int x25_release(struct socket *sock) break; case X25_STATE_5: - x25_write_internal(sk, X25_CLEAR_REQUEST); + x25_write_internal_owned(sk, X25_CLEAR_REQUEST); x25_disconnect(sk, 0, 0, 0); __x25_destroy_socket(sk); goto out; @@ -805,7 +879,7 @@ static int x25_connect(struct socket *sock, struct sockaddr_unsized *uaddr, x25->state = X25_STATE_1; - x25_write_internal(sk, X25_CALL_REQUEST); + x25_write_internal_owned(sk, X25_CALL_REQUEST); x25_start_heartbeat(sk); x25_start_t21timer(sk); @@ -1257,7 +1331,7 @@ static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) len++; } - x25_kick(sk); + x25_kick_owned(sk); rc = len; out: release_sock(sk); @@ -1359,7 +1433,7 @@ static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, msg->msg_namelen = sizeof(*sx25); } - x25_check_rbuf(sk); + x25_check_rbuf_owned(sk); rc = copied; out_free_dgram: skb_free_datagram(sk, skb); @@ -1598,7 +1672,7 @@ static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) /* must call accptapprv above */ if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags)) goto out_sendcallaccpt_release; - x25_write_internal(sk, X25_CALL_ACCEPTED); + x25_write_internal_owned(sk, X25_CALL_ACCEPTED); x25->state = X25_STATE_3; rc = 0; out_sendcallaccpt_release: @@ -1762,6 +1836,23 @@ static struct notifier_block x25_dev_notifier = { .notifier_call = x25_device_event, }; +/* May run in softirq, so lock_sock() is not an option. */ +static void x25_kill_sock(struct sock *sk, struct x25_neigh *nb) +{ + struct x25_sock *x25 = x25_sk(sk); + + local_bh_disable(); + bh_lock_sock(sk); + if (x25->neighbour == nb) { + if (sock_owned_by_user(sk)) + set_bit(X25_KILL_FLAG, &x25->flags); + else + x25_disconnect(sk, ENETUNREACH, 0, 0); + } + bh_unlock_sock(sk); + local_bh_enable(); +} + void x25_kill_by_neigh(struct x25_neigh *nb) { struct sock *s; @@ -1770,13 +1861,11 @@ void x25_kill_by_neigh(struct x25_neigh *nb) write_lock_bh(&x25_list_lock); sk_for_each(s, &x25_list) { - if (x25_sk(s)->neighbour == nb) { + if (x25_sk(s)->neighbour == nb && + !test_bit(X25_KILL_FLAG, &x25_sk(s)->flags)) { sock_hold(s); write_unlock_bh(&x25_list_lock); - lock_sock(s); - if (x25_sk(s)->neighbour == nb) - x25_disconnect(s, ENETUNREACH, 0, 0); - release_sock(s); + x25_kill_sock(s, nb); sock_put(s); goto again; } diff --git a/net/x25/x25_in.c b/net/x25/x25_in.c index e47ebd8acd21bff8d8fa1f5f48163145e9ac00f2..297acfdbfb05da00894651bb7ee27d8753ef3339 100644 --- a/net/x25/x25_in.c +++ b/net/x25/x25_in.c @@ -450,7 +450,12 @@ int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb) int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb) { - int queued = x25_process_rx_frame(sk, skb); + int queued; + + /* Serialize against x25_kill_sock() like the softirq receive path */ + spin_lock_bh(&sk->sk_lock.slock); + queued = x25_process_rx_frame(sk, skb); + spin_unlock_bh(&sk->sk_lock.slock); if (!queued) kfree_skb(skb); diff --git a/net/x25/x25_out.c b/net/x25/x25_out.c index f8922b0e23a4f17c1f5f74aa24806a35325e92dd..f710d015fcc2ffd6a6922a0c29876bb248125666 100644 --- a/net/x25/x25_out.c +++ b/net/x25/x25_out.c @@ -140,7 +140,8 @@ void x25_kick(struct sock *sk) int modulus; struct x25_sock *x25 = x25_sk(sk); - if (x25->state != X25_STATE_3) + if (x25->state != X25_STATE_3 || + test_bit(X25_KILL_FLAG, &x25->flags)) return; /* diff --git a/net/x25/x25_subr.c b/net/x25/x25_subr.c index 159708d9ad20cb2e6db24ead67daf1e9d6258f64..dba3b8eb10b0f21402592fe5775e1670f68ed4ee 100644 --- a/net/x25/x25_subr.c +++ b/net/x25/x25_subr.c @@ -120,6 +120,10 @@ void x25_write_internal(struct sock *sk, int frametype) */ int len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN; + /* The call has already been cleared by a link failure or restart */ + if (test_bit(X25_KILL_FLAG, &x25->flags)) + return; + /* * Adjust frame size. */ -- 2.47.3