From: Bryam Vargas A HiperSockets socket is IUCV_BOUND from bind() until the SYN|ACK arrives, with or without a SYN on its way, and nothing tells the two apart: - SO_MSGLIMIT is accepted after the SYN carried msglimit to the peer as the receive window, while recvmsg() counts the frames it reads against the new value. When the limit is lowered, a peer that keeps to its window has the socket closed on it once the receive buffer is full and frames wait in backlog_skb_q: WARN_ON(), -EFAULT, the rest of the stream lost. When it is raised far enough, no window update is sent and the peer's writer waits for one that only a data frame from us would carry. - A later connect() sends another SYN: one restarted after a signal, or one that follows a non-blocking or timed-out connect(). The listener makes a second child with the same names. - A SYN|ACK or SYN|FIN is accepted by a socket that sent no SYN. Record that the SYN was handed to the device. From then on SO_MSGLIMIT fails with -EINVAL, as on a connected socket, and connect() sends no other SYN: it waits for the reply to the first, or fails with -EALREADY if it names another peer. A SYN|ACK or SYN|FIN for a socket that sent no SYN is dropped. A SYN that was not sent records nothing. Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport") Fixes: 800c5eb7b5eb ("af_iucv: change net_device handling for HS transport") Suggested-by: Alexandra Winter Cc: stable@vger.kernel.org # 6.18.x Signed-off-by: Bryam Vargas --- v5 (Alexandra's review of v4 1/8, [8] in the cover letter): - new patch in this place. v4 1/8 kept the window given to the peer in a field of its own. This one records only that the SYN left, as Alexandra proposed: once it has, SO_MSGLIMIT fails, connect() sends no other SYN, and a SYN|ACK or SYN|FIN for a socket that sent no SYN is dropped. The window field and its helper are gone. 28 lines added and 3 removed, against 33 and 3 - a later connect() to the same peer waits for the reply to the first SYN. Alexandra preferred that to refusing it [9] v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-1-f96c506447ce@proton.me/ v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-1-35677b68e697@proton.me/ It goes first because the backlog of 6/10 counts frames against msglimit, which must then be the window the SYN gave. Emulated setup, with the reply to the SYN held back in the adapter where the case needs it: - SO_MSGLIMIT 8 when the SYN goes out, lowered to 2, minimum SO_RCVBUF, the peer sends 8 frames. net reads 2, then takes the WARN_ON() in iucv_sock_recvmsg() and returns -EFAULT. With the patch setsockopt() fails with -EINVAL and all 8 are read. - SO_MSGLIMIT 2 when the SYN goes out, raised to 8. On net the peer's writer stops after 2 frames. With the patch setsockopt() fails and all frames arrive. - The same without a hold: a non-blocking connect() and SO_MSGLIMIT right after it, 20 times, with the adapter interruption delayed. net accepts the option with the SYN out in 20 of 20; with the series it fails in 20 of 20, in one of them with the reply already in. With no delay the reply is in before connect() returns and the option fails on both, as on a connected socket. - A second connect() to the same peer before the reply. net sends a second SYN and the listener makes two children; with the patch one SYN and one child, and the non-blocking call returns -EAGAIN as on net. - connect() interrupted by a signal and called again, connect() restarted by the kernel (SO_SNDTIMEO 0, SA_RESTART), and connect() called again after its timeout: two children on net in the first two and three in the last. With the patch one child in each, and the later call returns 0 when the SYN|ACK comes in. - A second connect() that names another peer: net sends a SYN to each; with the patch it fails with -EALREADY, no SYN goes to the second peer and the first still connects. - A forged SYN|ACK for a socket that only called bind(): net connects it and reads the byte sent after it; with the patch the frame is dropped and the socket stays IUCV_BOUND. A forged SYN|FIN: net leaves the socket in IUCV_DISCONN and listen() fails with -EINVAL; with the patch listen() works. - connect() with the interface down: -ENETDOWN, nothing is recorded, and SO_MSGLIMIT is still accepted afterwards, as on net. With the refusal instead of the wait (-EALREADY for any connect() once the SYN is out): the call made again after -EINTR or after the timeout returns -EALREADY and the socket connects later; with SO_SNDTIMEO 0 and SA_RESTART the one connect() the caller made returns -EALREADY. One child in each. Six pieces of this patch were each taken out or changed in a kernel of its own, once on top of the patch alone and once on top of the series: twelve kernels, which the cover letter counts as 6. Each of the twelve fails the case that covers its piece. What this patch costs: a socket sends one SYN, so a socket whose SYN got no reply cannot connect any more. With the first SYN dropped in the adapter and SO_SNDTIMEO 300 ms, net connects on the second connect(); with the patch the second call times out as well, and a new socket bound to the same name connects. 10/10 gives the retry back when the device reports that the SYN had no peer. A SYN that was delivered and not answered, or lost with no report, still needs a new socket. Not closed by this patch alone: a reply that comes in while the transmit of the SYN fails is still accepted until 6/10 serializes the two. connect() still tests sk_state before lock_sock(). A socket that got connected in between finds its SYN recorded and sends no other. --- include/net/iucv/af_iucv.h | 1 + net/iucv/af_iucv.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/net/iucv/af_iucv.h b/include/net/iucv/af_iucv.h index df85d19fbf84..ecddce19dcb2 100644 --- a/include/net/iucv/af_iucv.h +++ b/include/net/iucv/af_iucv.h @@ -130,6 +130,7 @@ struct iucv_sock { u8 flags; u16 msglimit; u16 msglimit_peer; + bool syn_sent; /* HiperSockets: our SYN left */ atomic_t skbs_in_xmit; atomic_t msg_sent; atomic_t msg_recv; diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c index ce13fe3c85b5..0c69fd661431 100644 --- a/net/iucv/af_iucv.c +++ b/net/iucv/af_iucv.c @@ -255,11 +255,17 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN; } + /* Set before the transmit: the reply can be in before it returns */ + if (flags == AF_IUCV_FLAG_SYN) + WRITE_ONCE(iucv->syn_sent, true); + atomic_inc(&iucv->skbs_in_xmit); err = dev_queue_xmit(skb); if (net_xmit_eval(err)) { atomic_dec(&iucv->skbs_in_xmit); atomic_add(confirm_recv, &iucv->msg_recv); + if (flags == AF_IUCV_FLAG_SYN) + WRITE_ONCE(iucv->syn_sent, false); } return net_xmit_eval(err); @@ -770,6 +776,15 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr, lock_sock(sk); + /* A HiperSockets socket sends one SYN: wait for the reply to it */ + if (iucv->syn_sent) { + err = 0; + if (memcmp(iucv->dst_user_id, sa->siucv_user_id, 8) || + memcmp(iucv->dst_name, sa->siucv_name, 8)) + err = -EALREADY; + goto sent; + } + /* Set the destination information */ memcpy(iucv->dst_user_id, sa->siucv_user_id, 8); memcpy(iucv->dst_name, sa->siucv_name, 8); @@ -778,6 +793,7 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr, err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN); else err = afiucv_path_connect(sock, addr); +sent: if (err) goto done; @@ -1544,8 +1560,14 @@ static int iucv_sock_setsockopt(struct socket *sock, int level, int optname, break; case SO_MSGLIMIT: switch (sk->sk_state) { - case IUCV_OPEN: case IUCV_BOUND: + /* the SYN gave the peer msglimit as its window */ + if (iucv->syn_sent) { + rc = -EINVAL; + break; + } + fallthrough; + case IUCV_OPEN: if (val < 1 || val > U16_MAX) rc = -EINVAL; else @@ -1947,7 +1969,8 @@ static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb) { struct iucv_sock *iucv = iucv_sk(sk); - if (!iucv || sk->sk_state != IUCV_BOUND) { + if (!iucv || sk->sk_state != IUCV_BOUND || + !READ_ONCE(iucv->syn_sent)) { kfree_skb(skb); return NET_RX_SUCCESS; } @@ -1968,7 +1991,8 @@ static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb) { struct iucv_sock *iucv = iucv_sk(sk); - if (!iucv || sk->sk_state != IUCV_BOUND) { + if (!iucv || sk->sk_state != IUCV_BOUND || + !READ_ONCE(iucv->syn_sent)) { kfree_skb(skb); return NET_RX_SUCCESS; } -- 2.56.1