sctp: Start shutdown on association restart if in SHUTDOWN-SENT state and socket is closed
Subsystems (3)
PATCH 1/1 sctp: Start shutdown on association restart if in SHUTDOWN-SENT state and socket is closed
Q29tbWl0IGJkZjZmYTUyZjAxYiAoInNjdHA6IGhhbmRsZSBhc3NvY2lhdGlvbiByZXN0YXJ0cyB3 aGVuIHRoZQpzb2NrZXQgaXMgY2xvc2VkLiIpIHN0YXJ0cyBzaHV0ZG93biB3aGVuIGFuIGFzc29j aWF0aW9uIGlzIHJlc3RhcnRlZCwKaWYgaW4gU0hVVERPV04tUEVORElORyBzdGF0ZSBhbmQgdGhl IHNvY2tldCBpcyBjbG9zZWQuIEhvd2V2ZXIsIHRoZQpyYXRpb25hbGUgc3RhdGVkIGluIHRoYXQg Y29tbWl0IGFwcGxpZXMgYWxzbyB3aGVuIGluIFNIVVRET1dOLVNFTlQKc3RhdGUgLSB3ZSBkb24n dCB3YW50IHRvIG1vdmUgYW4gYXNzb2NpYXRpb24gdG8gRVNUQUJMSVNIRUQgc3RhdGUgd2hlbgp0 aGUgc29ja2V0IGhhcyBiZWVuIGNsb3NlZCwgYmVjYXVzZSB0aGF0IHJlc3VsdHMgaW4gYW4gYXNz b2NpYXRpb24KdGhhdCBpcyB1bnJlYWNoYWJsZSBmcm9tIHVzZXIgc3BhY2UuCgpUaGUgcHJvYmxl bSBzY2VuYXJpbzoKCjEuICBDbGllbnQgY3Jhc2hlcyBhbmQvb3IgcmVzdGFydHMuCgoyLiAgU2Vy dmVyICh1c2luZyBvbmUtdG8tb25lIHNvY2tldCkgY2FsbHMgY2xvc2UoKS4gU0hVVERPV04gaXMg bG9zdC4KCjMuICBDbGllbnQgcmVjb25uZWN0cyB1c2luZyB0aGUgc2FtZSBhZGRyZXNzZXMgYW5k IHBvcnRzLgoKNC4gIFNlcnZlcidzIGFzc29jaWF0aW9uIGlzIHJlc3RhcnRlZC4gVGhlIGFzc29j aWF0aW9uIGFuZCB0aGUgc29ja2V0CiAgICBtb3ZlIHRvIEVTV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IHdpdGggQ09PS0lFX0FDSy4KKwkJLyog SWYgdGhlIHNvY2tldCBoYXMgYmVlbiBjbG9zZWQgYnkgdXNlciwgZG9uJ3QKKwkJICogdHJhbnNp dGlvbiB0byBFU1RBQkxJU0hFRC4gSW5zdGVhZCB0cmlnZ2VyIFNIVVRET1dOCisJCSAqIGJ1bmRs ZWQgd2l0aCBDT09LSUVfQUNLLgogCQkgKi8KIAkJc2N0cF9hZGRfY21kX3NmKGNvbW1hbmRzLCBT Q1RQX0NNRF9SRVBMWSwgU0NUUF9DSFVOSyhyZXBsKSk7CiAJCXJldHVybiBzY3RwX3NmX2RvXzlf Ml9zdGFydF9zaHV0ZG93bihuZXQsIGVwLCBhc29jLAoKYmFzZS1jb21taXQ6IDIwYTc4NWFhNTJj ODIyNDYwNTVhMDg5ZTU1ZGY5ZGFjNDdkNjdkYTEKLS0gCjIuMjUuMgoK
Quoted 60 lines · Jere Leppänen
Commit
bdf6fa52f01b("sctp: handle association restarts when the socket is closed.") starts shutdown when an association is restarted, if in SHUTDOWN-PENDING state and the socket is closed. However, the rationale stated in that commit applies also when in SHUTDOWN-SENT state - we don't want to move an association to ESTABLISHED state when the socket has been closed, because that results in an association that is unreachable from user space.The problem scenario:
1. Client crashes and/or restarts.
2. Server (using one-to-one socket) calls
close(). SHUTDOWN is lost.3. Client reconnects using the same addresses and ports.
4. Server's association is restarted. The association and the socket move to ESTABLISHED state, even though the server process has closed its descriptor.
Also, after step 4 when the server process exits, some resources are leaked in an attempt to release the underlying inet sock structure in ESTABLISHED state:
IPv4: Attempt to release TCP socket in state 1 00000000377288c7Fix by acting the same way as in SHUTDOWN-PENDING state. That is, if an association is restarted in SHUTDOWN-SENT state and the socket is closed, then start shutdown and don't move the association or the socket to ESTABLISHED state.
Fixes bdf6fa52f01b ("sctp: handle association restarts when the socket is closed.")Signed-off-by Jere Leppänen <[email protected]>--- net/sctp/sm_statefuns.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)net/sctp/sm_statefuns.c +5
-4@@ -1856,12 +1856,13 @@ static enum sctp_disposition sctp_sf_do_dupcook_a(/* Update the content of current association. */ sctp_add_cmd_sf (commands,SCTP_CMD_UPDATE_ASSOC ,SCTP_ASOC (new_asoc));sctp_add_cmd_sf (commands,SCTP_CMD_EVENT_ULP ,SCTP_ULPEVENT (ev)); -if (sctp_state (asoc,SHUTDOWN_PENDING ) && +if ((sctp_state (asoc,SHUTDOWN_PENDING ) || +sctp_state (asoc,SHUTDOWN_SENT )) && (sctp_sstate (asoc->base.sk,CLOSING ) ||sock_flag (asoc->base.sk,SOCK_DEAD ))) { -/* if were currently in SHUTDOWN_PENDING, but the socket -* has been closed by user, don't transition to ESTABLISHED. -* Instead trigger SHUTDOWN bundled with COOKIE_ACK. +/* If the socket has been closed by user, don't +* transition to ESTABLISHED. Instead trigger SHUTDOWN +* bundled with COOKIE_ACK. */ sctp_add_cmd_sf (commands,SCTP_CMD_REPLY ,SCTP_CHUNK (repl)); return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
Quoted 1 line
base-commit:
20a785aa52c82246055a089e55df9dac47d67da1
This last line is not standard, but git didn't complain about it here.
Quoted 61 lines · Marcelo Ricardo Leitner
Quoted 60 lines · Jere Leppänen
Commit
bdf6fa52f01b("sctp: handle association restarts when the socket is closed.") starts shutdown when an association is restarted, if in SHUTDOWN-PENDING state and the socket is closed. However, the rationale stated in that commit applies also when in SHUTDOWN-SENT state - we don't want to move an association to ESTABLISHED state when the socket has been closed, because that results in an association that is unreachable from user space.The problem scenario:
1. Client crashes and/or restarts.
2. Server (using one-to-one socket) calls
close(). SHUTDOWN is lost.3. Client reconnects using the same addresses and ports.
4. Server's association is restarted. The association and the socket move to ESTABLISHED state, even though the server process has closed its descriptor.
Also, after step 4 when the server process exits, some resources are leaked in an attempt to release the underlying inet sock structure in ESTABLISHED state:
IPv4: Attempt to release TCP socket in state 1 00000000377288c7Fix by acting the same way as in SHUTDOWN-PENDING state. That is, if an association is restarted in SHUTDOWN-SENT state and the socket is closed, then start shutdown and don't move the association or the socket to ESTABLISHED state.
Fixes bdf6fa52f01b ("sctp: handle association restarts when the socket is closed.")Signed-off-by Jere Leppänen <[email protected]>--- net/sctp/sm_statefuns.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)net/sctp/sm_statefuns.c +5
-4@@ -1856,12 +1856,13 @@ static enum sctp_disposition sctp_sf_do_dupcook_a(/* Update the content of current association. */ sctp_add_cmd_sf (commands,SCTP_CMD_UPDATE_ASSOC ,SCTP_ASOC (new_asoc));sctp_add_cmd_sf (commands,SCTP_CMD_EVENT_ULP ,SCTP_ULPEVENT (ev)); -if (sctp_state (asoc,SHUTDOWN_PENDING ) && +if ((sctp_state (asoc,SHUTDOWN_PENDING ) || +sctp_state (asoc,SHUTDOWN_SENT )) && (sctp_sstate (asoc->base.sk,CLOSING ) ||sock_flag (asoc->base.sk,SOCK_DEAD ))) { -/* if were currently in SHUTDOWN_PENDING, but the socket -* has been closed by user, don't transition to ESTABLISHED. -* Instead trigger SHUTDOWN bundled with COOKIE_ACK. +/* If the socket has been closed by user, don't +* transition to ESTABLISHED. Instead trigger SHUTDOWN +* bundled with COOKIE_ACK. */ sctp_add_cmd_sf (commands,SCTP_CMD_REPLY ,SCTP_CHUNK (repl)); return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
Quoted 3 lines
Quoted 1 line
base-commit:
20a785aa52c82246055a089e55df9dac47d67da1This last line is not standard, but git didn't complain about it here.
The git format-patch --base option. It's mentioned in the docs:
https://www.kernel.org/doc/html/v5.6/process/submitting-patches.html#providing-base-tree-information
Quoted 2 lines
Thanks for the speedy ack, Marcelo.
David Miller
maintainerFrom: Jere Leppänen <[email protected]>
Date: Wed, 20 May 2020 18:15:31 +0300
Quoted 26 lines
Commit
bdf6fa52f01b("sctp: handle association restarts when the socket is closed.") starts shutdown when an association is restarted, if in SHUTDOWN-PENDING state and the socket is closed. However, the rationale stated in that commit applies also when in SHUTDOWN-SENT state - we don't want to move an association to ESTABLISHED state when the socket has been closed, because that results in an association that is unreachable from user space.The problem scenario:
1. Client crashes and/or restarts.
2. Server (using one-to-one socket) calls
close(). SHUTDOWN is lost.3. Client reconnects using the same addresses and ports.
4. Server's association is restarted. The association and the socket move to ESTABLISHED state, even though the server process has closed its descriptor.
Also, after step 4 when the server process exits, some resources are leaked in an attempt to release the underlying inet sock structure in ESTABLISHED state:
IPv4: Attempt to release TCP socket in state 1 00000000377288c7
Quoted 7 lines
Fix by acting the same way as in SHUTDOWN-PENDING state. That is, if an association is restarted in SHUTDOWN-SENT state and the socket is closed, then start shutdown and don't move the association or the socket to ESTABLISHED state.
Fixes bdf6fa52f01b ("sctp: handle association restarts when the socket is closed.")Signed-off-by Jere Leppänen <[email protected]>
Applied and queued up for -stable, thanks.