Skip to content

Commit 4d5c70e

Browse files
lxindavem330
authored andcommitted
sctp: ensure sk_state is set to CLOSED if hashing fails in sctp_listen_start
If hashing fails in sctp_listen_start(), the socket remains in the LISTENING state, even though it was not added to the hash table. This can lead to a scenario where a socket appears to be listening without actually being accessible. This patch ensures that if the hashing operation fails, the sk_state is set back to CLOSED before returning an error. Note that there is no need to undo the autobind operation if hashing fails, as the bind port can still be used for next listen() call on the same socket. Fixes: 76c6d98 ("sctp: add sock_reuseport for the sock in __sctp_hash_endpoint") Reported-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: Xin Long <[email protected]> Acked-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 82c5b53 commit 4d5c70e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

net/sctp/socket.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8531,6 +8531,7 @@ static int sctp_listen_start(struct sock *sk, int backlog)
85318531
struct sctp_endpoint *ep = sp->ep;
85328532
struct crypto_shash *tfm = NULL;
85338533
char alg[32];
8534+
int err;
85348535

85358536
/* Allocate HMAC for generating cookie. */
85368537
if (!sp->hmac && sp->sctp_hmac_alg) {
@@ -8558,18 +8559,25 @@ static int sctp_listen_start(struct sock *sk, int backlog)
85588559
inet_sk_set_state(sk, SCTP_SS_LISTENING);
85598560
if (!ep->base.bind_addr.port) {
85608561
if (sctp_autobind(sk)) {
8561-
inet_sk_set_state(sk, SCTP_SS_CLOSED);
8562-
return -EAGAIN;
8562+
err = -EAGAIN;
8563+
goto err;
85638564
}
85648565
} else {
85658566
if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8566-
inet_sk_set_state(sk, SCTP_SS_CLOSED);
8567-
return -EADDRINUSE;
8567+
err = -EADDRINUSE;
8568+
goto err;
85688569
}
85698570
}
85708571

85718572
WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
8572-
return sctp_hash_endpoint(ep);
8573+
err = sctp_hash_endpoint(ep);
8574+
if (err)
8575+
goto err;
8576+
8577+
return 0;
8578+
err:
8579+
inet_sk_set_state(sk, SCTP_SS_CLOSED);
8580+
return err;
85738581
}
85748582

85758583
/*

0 commit comments

Comments
 (0)