sctp: always set frag_point on pmtu change
Subsystems (3)
PATCH 1/1 sctp: always set frag_point on pmtu change
Calling send on a connected SCTP socket results in kernel panic if spp_pathmtu was configured manually before an association is established and it was not reconfigured to another value once the association is established.
Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockopt SCTP_PEER_ADDR_PARAMS with spp_pathmtu set to a legal value (e.g. 1000) and SPP_PMTUD_DISABLE set in spp_flags.
4. Connect client to server.
5. Send message from client to server.
At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memory
Commit 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point") introduces sctp_assoc_update_frag_point, but this function is not called in this case, causing frag_point to be zero:
void sctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu) {
- if (asoc->pathmtu != pmtu)
+ if (asoc->pathmtu != pmtu) {
asoc->pathmtu = pmtu;
+ sctp_assoc_update_frag_point(asoc);
+ }
In this scenario, on association establishment, asoc->pathmtu is already 1000 and pmtu will be as well. Before this commit the frag_point was being correctly set in the scenario described. Moving the call outside the if block fixes the issue.
I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("func_tests: frag_point should never be zero").
I have also taken the liberty to introduce a sanity check in chunk.c to set the frag_point to a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).
--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)
include/net/sctp/constants.h +3 -0
@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */
net/sctp/associola.c +7 -6
@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } +pr_debug ("%s: before asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }/* Update the association's pmtu and frag_point by going through all the
net/sctp/chunk.c +6 -0
@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ +if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;/* If the the peer requested that we authenticate DATA chunks
Signature · 1 line
2.17.1
Xin Long
maintainerQuoted 77 lines · Jakub Audykowicz
Calling send on a connected SCTP socket results in kernel panic if
spp_pathmtuwas configured manually before an association is established and it was not reconfigured to another value once the association is established.Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockoptSCTP_PEER_ADDR_PARAMSwithspp_pathmtuset to a legal value (e.g. 1000) andSPP_PMTUD_DISABLEset inspp_flags.
4. Connect client to server.
5. Send message from client to server.At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memoryCommit
2f5e3c9df693("sctp: introducesctp_assoc_update_frag_point") introducessctp_assoc_update_frag_point, but this function is not called in this case, causingfrag_pointto be zero:
voidsctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu){(quoted diff) +3
-1- if (asoc->pathmtu != pmtu) + if (asoc->pathmtu != pmtu) { asoc->pathmtu = pmtu; + sctp_assoc_update_frag_point(asoc); + }In this scenario, on association establishment,
asoc->pathmtuis already 1000 and pmtu will be as well. Before this commit thefrag_pointwas being correctly set in the scenario described. Moving the call outside the if block fixes the issue.I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("
func_tests:frag_pointshould never be zero").I have also taken the liberty to introduce a sanity check in
chunk.cto set thefrag_pointto a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).Fixes 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point")Signed-off-by Jakub Audykowicz <[email protected]>--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)include/net/sctp/constants.h +3
-0@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */ net/sctp/associola.c +1
-4@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } + pr_debug("%s: before asoc:%p, pmtu:%d, frag_point:%d\n",
net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1442
Quoted 10 lines
net/sctp/associola.c +6
-2+ __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }
The idea was whenever asoc->pathmtu changes, frag_point should be updated, but we missed one place in sctp_association_init().
Another issue is after 4-shakehand, the client's asoc->intl_enable may be changed from 0 to 1, which means the frag_point should also be updated, since [1]:
void sctp_assoc_update_frag_point(struct sctp_association *asoc)
{
int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu,
sctp_datachk_len(&asoc->stream)); <--- [1]So one fix for both issues is:
net/sctp/stream_interleave.c +0 -0
@@ -1327,4 +1327,5 @@ void sctp_stream_interleave_init(struct
sctp_stream *stream)
asoc = container_of(stream, struct sctp_association, stream);
stream->si = asoc->intl_enable ? &sctp_stream_interleave_1
: &sctp_stream_interleave_0;+ sctp_assoc_update_frag_point(asoc);
}
Quoted 7 lines
net/sctp/associola.c +0
-0/* Update the association's pmtu and frag_point by going through all the net/sctp/chunk.c +0
-0
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 10 lines
net/sctp/chunk.c +6
-0@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ +if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;
This won't happen if we sync frag_point on time like the above.
Quoted 124 lines · Xin Long
Quoted 76 lines · Jakub Audykowicz
Calling send on a connected SCTP socket results in kernel panic if
spp_pathmtuwas configured manually before an association is established and it was not reconfigured to another value once the association is established.Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockoptSCTP_PEER_ADDR_PARAMSwithspp_pathmtuset to a legal value (e.g. 1000) andSPP_PMTUD_DISABLEset inspp_flags.
4. Connect client to server.
5. Send message from client to server.At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memoryCommit
2f5e3c9df693("sctp: introducesctp_assoc_update_frag_point") introducessctp_assoc_update_frag_point, but this function is not called in this case, causingfrag_pointto be zero:
voidsctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu){(quoted diff) +3
-1- if (asoc->pathmtu != pmtu) + if (asoc->pathmtu != pmtu) { asoc->pathmtu = pmtu; + sctp_assoc_update_frag_point(asoc); + }In this scenario, on association establishment,
asoc->pathmtuis already 1000 and pmtu will be as well. Before this commit thefrag_pointwas being correctly set in the scenario described. Moving the call outside the if block fixes the issue.I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("
func_tests:frag_pointshould never be zero").I have also taken the liberty to introduce a sanity check in
chunk.cto set thefrag_pointto a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).Fixes 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point")Signed-off-by Jakub Audykowicz <[email protected]>--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)include/net/sctp/constants.h +3
-0@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */ net/sctp/associola.c +1
-4@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } + pr_debug("%s: before asoc:%p, pmtu:%d, frag_point:%d\n",net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1442
Quoted 10 lines
net/sctp/associola.c +6
-2+ __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }The idea was whenever
asoc->pathmtuchanges,frag_pointshould be updated, but we missed one place insctp_association_init().Another issue is after 4-shakehand, the client's
asoc->intl_enablemay be changed from 0 to 1, which means thefrag_pointshould also be updated, since [1]:void
sctp_assoc_update_frag_point(struct sctp_association *asoc){ int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu, sctp_datachk_len(&asoc->stream)); <--- [1]So one fix for both issues is:
net/sctp/stream_interleave.c +1
-0@@ -1327,4 +1327,5 @@ void sctp_stream_interleave_init(struct sctp_stream *stream) asoc = container_of(stream, struct sctp_association, stream); stream->si = asoc->intl_enable ? &sctp_stream_interleave_1 : &sctp_stream_interleave_0; +sctp_assoc_update_frag_point (asoc);}
Quoted 9 lines
net/sctp/associola.c +0
-0/* Update the association's pmtu and frag_point by going through all the net/sctp/chunk.c +0
-0@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */
Quoted 8 lines
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 7 lines
net/sctp/chunk.c +6
-0+if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;This won't happen if we sync
frag_pointon time like the above.
I know a better fix has been proposed and acked already but I would like to follow this up a bit. I still think there is some value to the changes in this patch. First of all, I am not sure the if statement in sctp_assoc_set_pmtu is of any practical benefit. I assume its intention is optimization, but I'm skeptical. Originally it made little sense, since it's not like assigning the same value would be incorrect or costly. Upon introducing sctp_assoc_update_frag_point I can see the if being of more use since it is theoretically useless to call this function if MTU is the same, but as it turns out it might still be useful at what I would estimate to be a negligible cost. I propose to do away with this if block.
As for the code in chunk, I'm not too proud of this hacky workaround, but I still think there should be some way to avoid chunking endlessly and running OOM if the configuration is wrong (here due to an implementation oversight), a last-ditch fail-safe of sorts. How do you guys think this could be accomplished?
Marcelo Ricardo Leitner
maintainerQuoted 135 lines · Jakub Audykowicz
Quoted 124 lines · Xin Long
Quoted 76 lines · Jakub Audykowicz
Calling send on a connected SCTP socket results in kernel panic if
spp_pathmtuwas configured manually before an association is established and it was not reconfigured to another value once the association is established.Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockoptSCTP_PEER_ADDR_PARAMSwithspp_pathmtuset to a legal value (e.g. 1000) andSPP_PMTUD_DISABLEset inspp_flags.
4. Connect client to server.
5. Send message from client to server.At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memoryCommit
2f5e3c9df693("sctp: introducesctp_assoc_update_frag_point") introducessctp_assoc_update_frag_point, but this function is not called in this case, causingfrag_pointto be zero:
voidsctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu){(quoted diff) +3
-1- if (asoc->pathmtu != pmtu) + if (asoc->pathmtu != pmtu) { asoc->pathmtu = pmtu; + sctp_assoc_update_frag_point(asoc); + }In this scenario, on association establishment,
asoc->pathmtuis already 1000 and pmtu will be as well. Before this commit thefrag_pointwas being correctly set in the scenario described. Moving the call outside the if block fixes the issue.I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("
func_tests:frag_pointshould never be zero").I have also taken the liberty to introduce a sanity check in
chunk.cto set thefrag_pointto a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).Fixes 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point")Signed-off-by Jakub Audykowicz <[email protected]>--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)include/net/sctp/constants.h +3
-0@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */ net/sctp/associola.c +1
-4@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } + pr_debug("%s: before asoc:%p, pmtu:%d, frag_point:%d\n",net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1442
Quoted 10 lines
net/sctp/associola.c +6
-2+ __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }The idea was whenever
asoc->pathmtuchanges,frag_pointshould be updated, but we missed one place insctp_association_init().Another issue is after 4-shakehand, the client's
asoc->intl_enablemay be changed from 0 to 1, which means thefrag_pointshould also be updated, since [1]:void
sctp_assoc_update_frag_point(struct sctp_association *asoc){ int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu, sctp_datachk_len(&asoc->stream)); <--- [1]So one fix for both issues is:
net/sctp/stream_interleave.c +1
-0@@ -1327,4 +1327,5 @@ void sctp_stream_interleave_init(struct sctp_stream *stream) asoc = container_of(stream, struct sctp_association, stream); stream->si = asoc->intl_enable ? &sctp_stream_interleave_1 : &sctp_stream_interleave_0; +sctp_assoc_update_frag_point (asoc);}
Quoted 9 lines
net/sctp/associola.c +0
-0/* Update the association's pmtu and frag_point by going through all the net/sctp/chunk.c +0
-0@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ Quoted 8 lines
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 7 lines
net/sctp/chunk.c +6
-0+if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;This won't happen if we sync
frag_pointon time like the above.
Quoted 2 lines
I know a better fix has been proposed and acked already but I would like to follow this up a bit. I still think there is some value to the changes in this
Any time. :-)
Quoted 4 lines
patch. First of all, I am not sure the if statement in
sctp_assoc_set_pmtuis of any practical benefit. I assume its intention is optimization, but I'm skeptical. Originally it made little sense, since it's not like assigning the same value would be incorrect or costly. Upon introducing
Agree. In SCTP stack usually we are not worried by the effects of a single assignment, especially in control path.
Quoted 4 lines
sctp_assoc_update_frag_pointI can see the if being of more use since it is theoretically useless to call this function if MTU is the same, but as it turns out it might still be useful at what I would estimate to be a negligible cost. I propose to do away with this if block.
Now it is more about consistency. If it is already set to a value and the user sets it again, there should be no behavioral difference. We don't want users calling the same function multiple times for it to "really stick" or so, for example.
The if in there, then, now serves to protect it from this.
Quoted 5 lines
As for the code in chunk, I'm not too proud of this hacky workaround, but I still think there should be some way to avoid chunking endlessly and running OOM if the configuration is wrong (here due to an implementation oversight), a last-ditch fail-safe of sorts. How do you guys think this could be
Good point.
Quoted 1 line
accomplished?
Considering that the idea is for sctp_datamsg_from_user to just consume asoc->frag_point, we can add this check right at the end of sctp_assoc_update_frag_point. So that we will always set it to a sane value, no matter what, and warn if it is bogus, for whatever reason. This way it will also warn right when it got set to a bad value.
Something like (just a draft):
net/sctp/associola.c +8 -1
@@ -1427,8 +1427,15 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc) frag =min_t (int, frag,SCTP_MAX_CHUNK_LEN -sctp_datachk_len (&asoc->stream)); + frag =SCTP_TRUNC4 (frag); - asoc->frag_point =SCTP_TRUNC4 (frag); +if (frag <SCTP_FRAG_POINT_MIN ) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is less than allowed (%d < %d). Forcing to the minimum value." , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); + frag =SCTP_FRAG_POINT_MIN ; + } + + asoc->frag_point = frag; }void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu)
Note that it should have only 1 pr_warn_ratelimited (no line breaks) and be rate limited because the application could exploit this to trigger endless warnings and having it logged multiple times won't help.
Thanks,
Marcelo
Marcelo Ricardo Leitner
maintainerQuoted 184 lines · Marcelo Ricardo Leitner
Quoted 135 lines · Jakub Audykowicz
Quoted 124 lines · Xin Long
Quoted 76 lines · Jakub Audykowicz
Calling send on a connected SCTP socket results in kernel panic if
spp_pathmtuwas configured manually before an association is established and it was not reconfigured to another value once the association is established.Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockoptSCTP_PEER_ADDR_PARAMSwithspp_pathmtuset to a legal value (e.g. 1000) andSPP_PMTUD_DISABLEset inspp_flags.
4. Connect client to server.
5. Send message from client to server.At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memoryCommit
2f5e3c9df693("sctp: introducesctp_assoc_update_frag_point") introducessctp_assoc_update_frag_point, but this function is not called in this case, causingfrag_pointto be zero:
voidsctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu){(quoted diff) +3
-1- if (asoc->pathmtu != pmtu) + if (asoc->pathmtu != pmtu) { asoc->pathmtu = pmtu; + sctp_assoc_update_frag_point(asoc); + }In this scenario, on association establishment,
asoc->pathmtuis already 1000 and pmtu will be as well. Before this commit thefrag_pointwas being correctly set in the scenario described. Moving the call outside the if block fixes the issue.I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("
func_tests:frag_pointshould never be zero").I have also taken the liberty to introduce a sanity check in
chunk.cto set thefrag_pointto a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).Fixes 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point")Signed-off-by Jakub Audykowicz <[email protected]>--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)include/net/sctp/constants.h +3
-0@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */ net/sctp/associola.c +1
-4@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } + pr_debug("%s: before asoc:%p, pmtu:%d, frag_point:%d\n",net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1442
Quoted 10 lines
net/sctp/associola.c +6
-2+ __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }The idea was whenever
asoc->pathmtuchanges,frag_pointshould be updated, but we missed one place insctp_association_init().Another issue is after 4-shakehand, the client's
asoc->intl_enablemay be changed from 0 to 1, which means thefrag_pointshould also be updated, since [1]:void
sctp_assoc_update_frag_point(struct sctp_association *asoc){ int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu, sctp_datachk_len(&asoc->stream)); <--- [1]So one fix for both issues is:
net/sctp/stream_interleave.c +1
-0@@ -1327,4 +1327,5 @@ void sctp_stream_interleave_init(struct sctp_stream *stream) asoc = container_of(stream, struct sctp_association, stream); stream->si = asoc->intl_enable ? &sctp_stream_interleave_1 : &sctp_stream_interleave_0; +sctp_assoc_update_frag_point (asoc);}
Quoted 9 lines
net/sctp/associola.c +0
-0/* Update the association's pmtu and frag_point by going through all the net/sctp/chunk.c +0
-0@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ Quoted 8 lines
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 7 lines
net/sctp/chunk.c +6
-0+if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;This won't happen if we sync
frag_pointon time like the above.Quoted 2 lines
I know a better fix has been proposed and acked already but I would like to follow this up a bit. I still think there is some value to the changes in this
Any time. :-)
Quoted 4 lines
patch. First of all, I am not sure the if statement in
sctp_assoc_set_pmtuis of any practical benefit. I assume its intention is optimization, but I'm skeptical. Originally it made little sense, since it's not like assigning the same value would be incorrect or costly. Upon introducingAgree. In SCTP stack usually we are not worried by the effects of a single assignment, especially in control path.
Quoted 4 lines
sctp_assoc_update_frag_pointI can see the if being of more use since it is theoretically useless to call this function if MTU is the same, but as it turns out it might still be useful at what I would estimate to be a negligible cost. I propose to do away with this if block.Now it is more about consistency. If it is already set to a value and the user sets it again, there should be no behavioral difference. We don't want users calling the same function multiple times for it to "really stick" or so, for example.
The if in there, then, now serves to protect it from this.
Quoted 5 lines
As for the code in chunk, I'm not too proud of this hacky workaround, but I still think there should be some way to avoid chunking endlessly and running OOM if the configuration is wrong (here due to an implementation oversight), a last-ditch fail-safe of sorts. How do you guys think this could be
Good point.
Quoted 1 line
accomplished?
Considering that the idea is for
sctp_datamsg_from_userto just consumeasoc->frag_point, we can add this check right at the end ofsctp_assoc_update_frag_point. So that we will always set it to a sane value, no matter what, and warn if it is bogus, for whatever reason. This way it will also warn right when it got set to a bad value.Something like (just a draft):
net/sctp/associola.c +0
-0
net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1435
Quoted 10 lines
net/sctp/associola.c +5
-1@@ -1427,8 +1427,15 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc) frag =min_t (int, frag,SCTP_MAX_CHUNK_LEN -sctp_datachk_len (&asoc->stream)); + frag =SCTP_TRUNC4 (frag); - asoc->frag_point =SCTP_TRUNC4 (frag); +if (frag <SCTP_FRAG_POINT_MIN ) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is less than allowed (%d < %d). Forcing to the minimum value." , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); + frag =SCTP_FRAG_POINT_MIN ;
We don't need a new define here because it should be bounded to the same values as user_frag is, like in sctp_setsockopt_maxseg.
net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1441
Quoted 6 lines
net/sctp/associola.c +3
-0+ } + + asoc->frag_point = frag; }void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu)
But please scratch this patch. It wouldn't have helped with the situation because the lack of calling it is what caused the issue in the first place. That said, original patch is the best way I think. Notes below still apply, though.
It is tough to find that silver line on how much of sanity checks the code should or should not do. My reasoning here is based on the impact this issue has. Maybe others have different opinions?
Quoted 9 lines
Note that it should have only 1
pr_warn_ratelimited(no line breaks) and be rate limited because the application could exploit this to trigger endless warnings and having it logged multiple times won't help.Thanks,
Marcelo
Quoted 202 lines · Marcelo Ricardo Leitner
Quoted 177 lines · Marcelo Ricardo Leitner
Quoted 134 lines · Jakub Audykowicz
Quoted 124 lines · Xin Long
Quoted 76 lines · Jakub Audykowicz
Calling send on a connected SCTP socket results in kernel panic if
spp_pathmtuwas configured manually before an association is established and it was not reconfigured to another value once the association is established.Steps to reproduce:
1. Set up a listening SCTP server socket.
2. Set up an SCTP client socket.
3. Configure client socket using setsockoptSCTP_PEER_ADDR_PARAMSwithspp_pathmtuset to a legal value (e.g. 1000) andSPP_PMTUD_DISABLEset inspp_flags.
4. Connect client to server.
5. Send message from client to server.At this point oom-killer is invoked, which will eventually lead to:
[ 5.197262] Out of memory and no killable processes... [ 5.198107] Kernel panic - not syncing: System is deadlocked on memoryCommit
2f5e3c9df693("sctp: introducesctp_assoc_update_frag_point") introducessctp_assoc_update_frag_point, but this function is not called in this case, causingfrag_pointto be zero:
voidsctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu){(quoted diff) +3
-1- if (asoc->pathmtu != pmtu) + if (asoc->pathmtu != pmtu) { asoc->pathmtu = pmtu; + sctp_assoc_update_frag_point(asoc); + }In this scenario, on association establishment,
asoc->pathmtuis already 1000 and pmtu will be as well. Before this commit thefrag_pointwas being correctly set in the scenario described. Moving the call outside the if block fixes the issue.I will be providing a separate patch to lksctp-tools with a simple test reproducing this problem ("
func_tests:frag_pointshould never be zero").I have also taken the liberty to introduce a sanity check in
chunk.cto set thefrag_pointto a non-negative value in order to avoid chunking endlessly (which is the reason for the eventual panic).Fixes 2f5e3c9df693 ("sctp: introduce sctp_assoc_update_frag_point")Signed-off-by Jakub Audykowicz <[email protected]>--- include/net/sctp/constants.h | 3 +++ net/sctp/associola.c | 13 +++++++------ net/sctp/chunk.c | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-)include/net/sctp/constants.h +3
-0@@ -293,6 +293,9 @@ enum { SCTP_MAX_GABS = 16 };*/ #define SCTP_DEFAULT_MINSEGMENT 512/* MTU size ... if no mtu disc */ +/* An association's fragmentation point should never be non-positive */ +#define SCTP_FRAG_POINT_MIN 1 +#define SCTP_SECRET_SIZE 32/* Number of octets in a 256 bits. */ #define SCTP_SIGNATURE_SIZE 20/* size of a SLA-1 signature */ net/sctp/associola.c +1
-4@@ -1431,13 +1431,14 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc)void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu) { -if (asoc->pathmtu != pmtu) { - asoc->pathmtu = pmtu; -sctp_assoc_update_frag_point (asoc); - } + pr_debug("%s: before asoc:%p, pmtu:%d, frag_point:%d\n",net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1442
Quoted 10 lines
net/sctp/associola.c +6
-2+ __func__, asoc, asoc->pathmtu, asoc->frag_point); + + asoc->pathmtu = pmtu; +sctp_assoc_update_frag_point (asoc); -pr_debug ("%s: asoc:%p, pmtu:%d, frag_point:%d\n" , __func__, asoc, - asoc->pathmtu, asoc->frag_point); +pr_debug ("%s: after asoc:%p, pmtu:%d, frag_point:%d\n" , + __func__, asoc, asoc->pathmtu, asoc->frag_point); }The idea was whenever
asoc->pathmtuchanges,frag_pointshould be updated, but we missed one place insctp_association_init().Another issue is after 4-shakehand, the client's
asoc->intl_enablemay be changed from 0 to 1, which means thefrag_pointshould also be updated, since [1]:void
sctp_assoc_update_frag_point(struct sctp_association *asoc){ int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu, sctp_datachk_len(&asoc->stream)); <--- [1]So one fix for both issues is:
net/sctp/stream_interleave.c +1
-0@@ -1327,4 +1327,5 @@ void sctp_stream_interleave_init(struct sctp_stream *stream) asoc = container_of(stream, struct sctp_association, stream); stream->si = asoc->intl_enable ? &sctp_stream_interleave_1 : &sctp_stream_interleave_0; +sctp_assoc_update_frag_point (asoc);}
Quoted 9 lines
net/sctp/associola.c +0
-0/* Update the association's pmtu and frag_point by going through all the net/sctp/chunk.c +0
-0@@ -190,6 +190,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ Quoted 8 lines
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 7 lines
net/sctp/chunk.c +6
-0+if (asoc->frag_point <SCTP_FRAG_POINT_MIN ) { +pr_warn ("%s: asoc:%p->frag_point is less than allowed (%d<%d)" , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); +pr_warn ("forcing minimum value to avoid chunking endlessly" ); + asoc->frag_point =SCTP_FRAG_POINT_MIN ; + } max_data = asoc->frag_point;This won't happen if we sync
frag_pointon time like the above.Quoted 2 lines
I know a better fix has been proposed and acked already but I would like to follow this up a bit. I still think there is some value to the changes in this
Any time. :-)
Quoted 4 lines
patch. First of all, I am not sure the if statement in
sctp_assoc_set_pmtuis of any practical benefit. I assume its intention is optimization, but I'm skeptical. Originally it made little sense, since it's not like assigning the same value would be incorrect or costly. Upon introducingAgree. In SCTP stack usually we are not worried by the effects of a single assignment, especially in control path.
Quoted 4 lines
sctp_assoc_update_frag_pointI can see the if being of more use since it is theoretically useless to call this function if MTU is the same, but as it turns out it might still be useful at what I would estimate to be a negligible cost. I propose to do away with this if block.Now it is more about consistency. If it is already set to a value and the user sets it again, there should be no behavioral difference. We don't want users calling the same function multiple times for it to "really stick" or so, for example.
The if in there, then, now serves to protect it from this.
Quoted 4 lines
As for the code in chunk, I'm not too proud of this hacky workaround, but I still think there should be some way to avoid chunking endlessly and running OOM if the configuration is wrong (here due to an implementation oversight), a last-ditch fail-safe of sorts. How do you guys think this could be
Good point.
Quoted 1 line
accomplished?
Considering that the idea is for
sctp_datamsg_from_userto just consumeasoc->frag_point, we can add this check right at the end ofsctp_assoc_update_frag_point. So that we will always set it to a sane value, no matter what, and warn if it is bogus, for whatever reason. This way it will also warn right when it got set to a bad value.Something like (just a draft):
net/sctp/associola.c +0
-0net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1435
Quoted 10 lines
net/sctp/associola.c +5
-1@@ -1427,8 +1427,15 @@ void sctp_assoc_update_frag_point(struct sctp_association *asoc) frag =min_t (int, frag,SCTP_MAX_CHUNK_LEN -sctp_datachk_len (&asoc->stream)); + frag =SCTP_TRUNC4 (frag); - asoc->frag_point =SCTP_TRUNC4 (frag); +if (frag <SCTP_FRAG_POINT_MIN ) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is less than allowed (%d < %d). Forcing to the minimum value." , + __func__, asoc, asoc->frag_point,SCTP_FRAG_POINT_MIN ); + frag =SCTP_FRAG_POINT_MIN ;We don't need a new define here because it should be bounded to the same values as
user_fragis, like insctp_setsockopt_maxseg.net/sctp/associola.c void sctp_assoc_update_frag_point(struct sctp_association *asoc) · line 1441
Quoted 6 lines
net/sctp/associola.c +3
-0+ } + + asoc->frag_point = frag; }void sctp_assoc_set_pmtu (struct sctp_association *asoc,__u32 pmtu)But please scratch this patch. It wouldn't have helped with the situation because the lack of calling it is what caused the issue in the first place. That said, original patch is the best way I think. Notes below still apply, though.
Quoted 12 lines
It is tough to find that silver line on how much of sanity checks the code should or should not do. My reasoning here is based on the impact this issue has. Maybe others have different opinions?
Quoted 8 lines
Note that it should have only 1
pr_warn_ratelimited(no line breaks) and be rate limited because the application could exploit this to trigger endless warnings and having it logged multiple times won't help.Thanks,
Marcelo
OK, let's forget about that "if" :)
Coming back to the sanity check, I came up with something like below, based on the code from sctp_setsockopt_maxseg, like you mentioned. I may have overcomplicated things since I didn't know how to accomplish the same thing without passing sctp_sock* to sctp_datamsg_from_user. I wanted to avoid calling sctp_min_frag_point unless absolutely necessary, so I just check the frag_point against the zero that is causing the eventual kernel panic.
include/net/sctp/sctp.h +11 -0
@@ -620,4 +620,15 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) return false; } +static inline __u16 sctp_data_chunk_size (struct sctp_association *asoc) +{ +return asoc ?sctp_datachk_len (&asoc->stream) : +sizeof (struct sctp_data_chunk ); +} + +static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize); +} +#endif /* __net_sctp_h__ */
include/net/sctp/structs.h +2 -1
@@ -543,7 +543,8 @@ struct sctp_datamsg {struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *,struct sctp_sndrcvinfo *, -struct iov_iter *); +struct iov_iter *, +struct sctp_sock *);void sctp_datamsg_free (struct sctp_datamsg *);void sctp_datamsg_put (struct sctp_datamsg *);void sctp_chunk_fail (struct sctp_chunk *,int error);
net/sctp/chunk.c +9 -1
@@ -164,7 +164,8 @@ static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chu*/ struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *asoc,struct sctp_sndrcvinfo *sinfo, -struct iov_iter *from) +struct iov_iter *from, +struct sctp_sock *sp) {size_t len, first_len, max_data, remaining;size_t msg_len =iov_iter_count (from);
@@ -173,6 +174,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,struct sctp_chunk *chunk;struct sctp_datamsg *msg;int err; +__u32 min_frag_point; msg =sctp_datamsg_new (GFP_KERNEL );if (!msg)
@@ -190,6 +192,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ +if (unlikely (asoc->frag_point =0 )) { + min_frag_point =sctp_min_frag_point (sp,sctp_data_chunk_size (asoc)); +pr_warn_ratelimited ("%s: asoc:%p frag_point is too low (%d < %d), using default minimum" , + __func__, asoc, asoc->frag_point, min_frag_point); + asoc->frag_point = min_frag_point; + } max_data = asoc->frag_point;/* If the the peer requested that we authenticate DATA chunks
net/sctp/socket.c +3 -5
@@ -1938,7 +1938,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,pr_debug ("%s: we associated primitively\n" , __func__); } - datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter); + datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter, sp);if (IS_ERR (datamsg)) { err =PTR_ERR (datamsg); goto err;
@@ -3321,11 +3321,9 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsignedif (val) {int min_len, max_len; -__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) : -sizeof (struct sctp_data_chunk ); +__u16 datasize =sctp_data_chunk_size (asoc); - min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)
Marcelo Ricardo Leitner
maintainerOn Tue, Dec 04, 2018 at 06:00:51PM +0100, Jakub Audykowicz wrote:
...
Quoted 5 lines
OK, let's forget about that "if" :)
Coming back to the sanity check, I came up with something like below, based on the code fromsctp_setsockopt_maxseg, like you mentioned. I may have overcomplicated things since I didn't know how to accomplish the same thing without passing sctp_sock* tosctp_datamsg_from_user.
Yep. More below.
Quoted 3 lines
I wanted to avoid calling
sctp_min_frag_pointunless absolutely necessary, so I just check thefrag_pointagainst the zero that is causing the eventual kernel panic.
Makes sense.
include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 626
Quoted 13 lines
include/net/sctp/sctp.h +4
-0@@ -620,4 +620,15 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t)return false; } +static inline __u16 sctp_data_chunk_size (struct sctp_association *asoc) +{ +return asoc ?sctp_datachk_len (&asoc->stream) : +sizeof (struct sctp_data_chunk );
I don't think we need another layer on top of data chunk sizes here. We don't need this asoc check by the sendmsg callpath because it cannot be NULL by then. That said, I think we have avoid this helper, for now at least.
One other way would be to include the check into sctp_datachk_len(), but we currently have 9 calls to that but only 1 requires the check.
As asoc is initialized and considering the fix we just had in this area, stream->si will also be initialized so you should be good to just call sctp_datachk_len() directly.
include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 632
Quoted 6 lines
include/net/sctp/sctp.h +6
-0+} + +static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize); +}
This is a good helper to have. Makes it clearer.
Quoted 31 lines
include/net/sctp/sctp.h +1
-0+#endif /* __net_sctp_h__ */ include/net/sctp/structs.h +2
-1@@ -543,7 +543,8 @@ struct sctp_datamsg {struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *,struct sctp_sndrcvinfo *, -struct iov_iter *); +struct iov_iter *, +struct sctp_sock *);void sctp_datamsg_free (struct sctp_datamsg *);void sctp_datamsg_put (struct sctp_datamsg *);void sctp_chunk_fail (struct sctp_chunk *,int error);net/sctp/chunk.c +2
-1@@ -164,7 +164,8 @@ static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chu*/ struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *asoc,struct sctp_sndrcvinfo *sinfo, -struct iov_iter *from) +struct iov_iter *from, +struct sctp_sock *sp) {size_t len, first_len, max_data, remaining;size_t msg_len =iov_iter_count (from);
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 177
Quoted 4 lines
net/sctp/chunk.c +1
-0@@ -173,6 +174,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,struct sctp_chunk *chunk;struct sctp_datamsg *msg;int err; +__u32 min_frag_point;
Reverse christmass tree.. swap the last two lines:
+ __u32 min_frag_point;
int err;
But I guess we don't need this var anymore:
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 195
Quoted 8 lines
net/sctp/chunk.c +1
-0msg =sctp_datamsg_new (GFP_KERNEL );if (!msg)@@ -190,6 +192,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ +if (unlikely (asoc->frag_point =0 )) {
!asoc->frag_point instead
You can get to sctp_sock here with: sctp_sk(asoc->base.sk)
struct sctp_sock *sp = sctp_sk(asoc->base.sk);
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 4 lines
net/sctp/chunk.c +4
-0+ min_frag_point =sctp_min_frag_point (sp,sctp_data_chunk_size (asoc)); +pr_warn_ratelimited ("%s: asoc:%p frag_point is too low (%d < %d), using default minimum" , + __func__, asoc, asoc->frag_point, min_frag_point); + asoc->frag_point = min_frag_point;
No no. Lets not fix up things here. If we do this assignment, we may make the disparity even worse.
With that, we can work only on max_data and avoid the need of min_frag_point.
max_data = asoc->frag_point;
if (unlikely(!max_data)) {
...
max_data = ...
}Quoted 32 lines
net/sctp/chunk.c +1
-0+ } max_data = asoc->frag_point;/* If the the peer requested that we authenticate DATA chunks net/sctp/socket.c +3
-5@@ -1938,7 +1938,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,pr_debug ("%s: we associated primitively\n" , __func__); } - datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter); + datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter, sp);if (IS_ERR (datamsg)) { err =PTR_ERR (datamsg); goto err;@@ -3321,11 +3321,9 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsignedif (val) {int min_len, max_len; -__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) : -sizeof (struct sctp_data_chunk ); +__u16 datasize =sctp_data_chunk_size (asoc); - min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)
Quoted 138 lines · Marcelo Ricardo Leitner
On Tue, Dec 04, 2018 at 06:00:51PM +0100, Jakub Audykowicz wrote:
...Quoted 5 lines
OK, let's forget about that "if" :)
Coming back to the sanity check, I came up with something like below, based on the code fromsctp_setsockopt_maxseg, like you mentioned. I may have overcomplicated things since I didn't know how to accomplish the same thing without passing sctp_sock* tosctp_datamsg_from_user.Yep. More below.
Quoted 3 lines
I wanted to avoid calling
sctp_min_frag_pointunless absolutely necessary, so I just check thefrag_pointagainst the zero that is causing the eventual kernel panic.Makes sense.
include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 626
Quoted 13 lines
include/net/sctp/sctp.h +4
-0@@ -620,4 +620,15 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t)return false; } +static inline __u16 sctp_data_chunk_size (struct sctp_association *asoc) +{ +return asoc ?sctp_datachk_len (&asoc->stream) : +sizeof (struct sctp_data_chunk );I don't think we need another layer on top of data chunk sizes here. We don't need this asoc check by the sendmsg callpath because it cannot be NULL by then. That said, I think we have avoid this helper, for now at least.
One other way would be to include the check into
sctp_datachk_len(), but we currently have 9 calls to that but only 1 requires the check.As asoc is initialized and considering the fix we just had in this area,
stream->siwill also be initialized so you should be good to just callsctp_datachk_len()directly.include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 632
Quoted 6 lines
include/net/sctp/sctp.h +6
-0+} + +static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize); +}This is a good helper to have. Makes it clearer.
Quoted 31 lines
include/net/sctp/sctp.h +1
-0+#endif /* __net_sctp_h__ */ include/net/sctp/structs.h +2
-1@@ -543,7 +543,8 @@ struct sctp_datamsg {struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *,struct sctp_sndrcvinfo *, -struct iov_iter *); +struct iov_iter *, +struct sctp_sock *);void sctp_datamsg_free (struct sctp_datamsg *);void sctp_datamsg_put (struct sctp_datamsg *);void sctp_chunk_fail (struct sctp_chunk *,int error);net/sctp/chunk.c +2
-1@@ -164,7 +164,8 @@ static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chu*/ struct sctp_datamsg *sctp_datamsg_from_user (struct sctp_association *asoc,struct sctp_sndrcvinfo *sinfo, -struct iov_iter *from) +struct iov_iter *from, +struct sctp_sock *sp) {size_t len, first_len, max_data, remaining;size_t msg_len =iov_iter_count (from);net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 177
Quoted 4 lines
net/sctp/chunk.c +1
-0@@ -173,6 +174,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,struct sctp_chunk *chunk;struct sctp_datamsg *msg;int err; +__u32 min_frag_point;Reverse christmass tree.. swap the last two lines:
(quoted diff) +1
-0+ __u32 min_frag_point; int err;But I guess we don't need this var anymore:
net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 195
Quoted 8 lines
net/sctp/chunk.c +1
-0msg =sctp_datamsg_new (GFP_KERNEL );if (!msg)@@ -190,6 +192,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,/* This is the biggest possible DATA chunk that can fit into * the packet */ +if (unlikely (asoc->frag_point =0 )) {!asoc->frag_point insteadYou can get to
sctp_sockhere with:sctp_sk(asoc->base.sk)struct sctp_sock *sp = sctp_sk(asoc->base.sk);net/sctp/chunk.c struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, · line 199
Quoted 4 lines
net/sctp/chunk.c +4
-0+ min_frag_point =sctp_min_frag_point (sp,sctp_data_chunk_size (asoc)); +pr_warn_ratelimited ("%s: asoc:%p frag_point is too low (%d < %d), using default minimum" , + __func__, asoc, asoc->frag_point, min_frag_point); + asoc->frag_point = min_frag_point;No no. Lets not fix up things here. If we do this assignment, we may make the disparity even worse.
With that, we can work only onmax_dataand avoid the need ofmin_frag_point.max_data = asoc->frag_point; if (unlikely(!max_data)) { ... max_data = ... }Quoted 25 lines
net/sctp/chunk.c +1
-0+ } max_data = asoc->frag_point;/* If the the peer requested that we authenticate DATA chunks net/sctp/socket.c +2
-3@@ -1938,7 +1938,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,pr_debug ("%s: we associated primitively\n" , __func__); } - datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter); + datamsg =sctp_datamsg_from_user (asoc, sinfo, &msg->msg_iter, sp);if (IS_ERR (datamsg)) { err =PTR_ERR (datamsg); goto err;@@ -3321,11 +3321,9 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsignedif (val) {int min_len, max_len; -__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) : -sizeof (struct sctp_data_chunk ); +__u16 datasize =sctp_data_chunk_size (asoc);
Quoted 7 lines
net/sctp/socket.c static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned · line 3329
Quoted 7 lines
net/sctp/socket.c +1
-2- min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)
Thanks, I've taken your remarks into account and ended up with this simple solution:
include/net/sctp/sctp.h +5 -0
@@ -620,4 +620,9 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) return false; } +static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize); +} +#endif /* __net_sctp_h__ */
net/sctp/chunk.c +6 -0
@@ -191,6 +191,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,* the packet */ max_data = asoc->frag_point; +if (unlikely (!max_data)) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is zero, forcing max_data to default minimum" , + __func__, asoc); + max_data =sctp_min_frag_point ( +sctp_sk (asoc->base.sk),sctp_datachk_len (&asoc->stream)); + }/* If the the peer requested that we authenticate DATA chunks * we need to account for bundling of the AUTH chunks along with
net/sctp/socket.c +1 -2
@@ -3324,8 +3324,7 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) :sizeof (struct sctp_data_chunk ); - min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)
Â
Marcelo Ricardo Leitner
maintainerOn Tue, Dec 04, 2018 at 07:51:54PM +0100, Jakub Audykowicz wrote:
...
Quoted 2 lines
Thanks, I've taken your remarks into account and ended up with this simple solution:
LGTM! Thanks
include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 625
Quoted 12 lines
include/net/sctp/sctp.h +3
-0@@ -620,4 +620,9 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t)return false; } +static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize);
Not sure how final the patch is but be sure to run checkpatch.pl on it before submitting it officially. It flagged some issues like the above indentation using spaces, for example.
Quoted 37 lines
include/net/sctp/sctp.h +2
-0+} +#endif /* __net_sctp_h__ */ net/sctp/chunk.c +6
-0@@ -191,6 +191,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,* the packet */ max_data = asoc->frag_point; +if (unlikely (!max_data)) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is zero, forcing max_data to default minimum" , + __func__, asoc); + max_data =sctp_min_frag_point ( +sctp_sk (asoc->base.sk),sctp_datachk_len (&asoc->stream)); + }/* If the the peer requested that we authenticate DATA chunks * we need to account for bundling of the AUTH chunks along with net/sctp/socket.c +1
-2@@ -3324,8 +3324,7 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) :sizeof (struct sctp_data_chunk ); - min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)
Quoted 14 lines · Marcelo Ricardo Leitner
On Tue, Dec 04, 2018 at 07:51:54PM +0100, Jakub Audykowicz wrote:
...Quoted 2 lines
Thanks, I've taken your remarks into account and ended up with this simple solution:
LGTM! Thanks
Quoted 8 lines
include/net/sctp/sctp.h +0
-0@@ -620,4 +620,9 @@ static inline bool sctp_transport_pmtu_check(struct sctp_transport *t)return false; }
Quoted 6 lines
include/net/sctp/sctp.h static inline bool sctp_transport_pmtu_check(struct sctp_transport *t) · line 625
Quoted 3 lines
include/net/sctp/sctp.h +3
-0+static inline __u32 sctp_min_frag_point (struct sctp_sock *sp,__u16 datasize) +{ +return sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , datasize);Not sure how final the patch is but be sure to run checkpatch.pl on it before submitting it officially. It flagged some issues like the above indentation using spaces, for example.
Should be fine now. I have checkpatch as a post-commit hook but I was not committing when creating these quick diffs :).
Quoted 39 lines
Quoted 39 lines
include/net/sctp/sctp.h +2
-0+} +#endif /* __net_sctp_h__ */ net/sctp/chunk.c +6
-0@@ -191,6 +191,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,* the packet */ max_data = asoc->frag_point; +if (unlikely (!max_data)) { +pr_warn_ratelimited ("%s: asoc:%p frag_point is zero, forcing max_data to default minimum" , + __func__, asoc); + max_data =sctp_min_frag_point ( +sctp_sk (asoc->base.sk),sctp_datachk_len (&asoc->stream)); + }/* If the the peer requested that we authenticate DATA chunks * we need to account for bundling of the AUTH chunks along with net/sctp/socket.c +1
-2@@ -3324,8 +3324,7 @@ static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned__u16 datasize = asoc ?sctp_datachk_len (&asoc->stream) :sizeof (struct sctp_data_chunk ); - min_len =sctp_mtu_payload (sp,SCTP_DEFAULT_MINSEGMENT , - datasize); + min_len =sctp_min_frag_point (sp, datasize); max_len =SCTP_MAX_CHUNK_LEN - datasize;if (val < min_len || val > max_len)Â