Skip to content

Commit bbd951a

Browse files
Daniel BorkmannJiri Slaby
authored andcommitted
net: sctp: fix skb_over_panic when receiving malformed ASCONF chunks
commit 9de7922 upstream. Commit 6f4c618 ("SCTP : Add paramters validity check for ASCONF chunk") added basic verification of ASCONF chunks, however, it is still possible to remotely crash a server by sending a special crafted ASCONF chunk, even up to pre 2.6.12 kernels: skb_over_panic: text:ffffffffa01ea1c3 len:31056 put:30768 head:ffff88011bd81800 data:ffff88011bd81800 tail:0x7950 end:0x440 dev:<NULL> ------------[ cut here ]------------ kernel BUG at net/core/skbuff.c:129! [...] Call Trace: <IRQ> [<ffffffff8144fb1c>] skb_put+0x5c/0x70 [<ffffffffa01ea1c3>] sctp_addto_chunk+0x63/0xd0 [sctp] [<ffffffffa01eadaf>] sctp_process_asconf+0x1af/0x540 [sctp] [<ffffffff8152d025>] ? _read_unlock_bh+0x15/0x20 [<ffffffffa01e0038>] sctp_sf_do_asconf+0x168/0x240 [sctp] [<ffffffffa01e3751>] sctp_do_sm+0x71/0x1210 [sctp] [<ffffffff8147645d>] ? fib_rules_lookup+0xad/0xf0 [<ffffffffa01e6b22>] ? sctp_cmp_addr_exact+0x32/0x40 [sctp] [<ffffffffa01e8393>] sctp_assoc_bh_rcv+0xd3/0x180 [sctp] [<ffffffffa01ee986>] sctp_inq_push+0x56/0x80 [sctp] [<ffffffffa01fcc42>] sctp_rcv+0x982/0xa10 [sctp] [<ffffffffa01d5123>] ? ipt_local_in_hook+0x23/0x28 [iptable_filter] [<ffffffff8148bdc9>] ? nf_iterate+0x69/0xb0 [<ffffffff81496d10>] ? ip_local_deliver_finish+0x0/0x2d0 [<ffffffff8148bf86>] ? nf_hook_slow+0x76/0x120 [<ffffffff81496d10>] ? ip_local_deliver_finish+0x0/0x2d0 [<ffffffff81496ded>] ip_local_deliver_finish+0xdd/0x2d0 [<ffffffff81497078>] ip_local_deliver+0x98/0xa0 [<ffffffff8149653d>] ip_rcv_finish+0x12d/0x440 [<ffffffff81496ac5>] ip_rcv+0x275/0x350 [<ffffffff8145c88b>] __netif_receive_skb+0x4ab/0x750 [<ffffffff81460588>] netif_receive_skb+0x58/0x60 This can be triggered e.g., through a simple scripted nmap connection scan injecting the chunk after the handshake, for example, ... -------------- INIT[ASCONF; ASCONF_ACK] -------------> <----------- INIT-ACK[ASCONF; ASCONF_ACK] ------------ -------------------- COOKIE-ECHO --------------------> <-------------------- COOKIE-ACK --------------------- ------------------ ASCONF; UNKNOWN ------------------> ... where ASCONF chunk of length 280 contains 2 parameters ... 1) Add IP address parameter (param length: 16) 2) Add/del IP address parameter (param length: 255) ... followed by an UNKNOWN chunk of e.g. 4 bytes. Here, the Address Parameter in the ASCONF chunk is even missing, too. This is just an example and similarly-crafted ASCONF chunks could be used just as well. The ASCONF chunk passes through sctp_verify_asconf() as all parameters passed sanity checks, and after walking, we ended up successfully at the chunk end boundary, and thus may invoke sctp_process_asconf(). Parameter walking is done with WORD_ROUND() to take padding into account. In sctp_process_asconf()'s TLV processing, we may fail in sctp_process_asconf_param() e.g., due to removal of the IP address that is also the source address of the packet containing the ASCONF chunk, and thus we need to add all TLVs after the failure to our ASCONF response to remote via helper function sctp_add_asconf_response(), which basically invokes a sctp_addto_chunk() adding the error parameters to the given skb. When walking to the next parameter this time, we proceed with ... length = ntohs(asconf_param->param_hdr.length); asconf_param = (void *)asconf_param + length; ... instead of the WORD_ROUND()'ed length, thus resulting here in an off-by-one that leads to reading the follow-up garbage parameter length of 12336, and thus throwing an skb_over_panic for the reply when trying to sctp_addto_chunk() next time, which implicitly calls the skb_put() with that length. Fix it by using sctp_walk_params() [ which is also used in INIT parameter processing ] macro in the verification *and* in ASCONF processing: it will make sure we don't spill over, that we walk parameters WORD_ROUND()'ed. Moreover, we're being more defensive and guard against unknown parameter types and missized addresses. Joint work with Vlad Yasevich. Fixes: b896b82 ("[SCTP] ADDIP: Support for processing incoming ASCONF_ACK chunks.") Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Vlad Yasevich <vyasevich@gmail.com> Acked-by: Neil Horman <nhorman@tuxdriver.com> Signed-off-by: David S. Miller <davem@davemloft.net> Cc: Josh Boyer <jwboyer@fedoraproject.org> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent a723db0 commit bbd951a

3 files changed

Lines changed: 60 additions & 63 deletions

File tree

‎include/net/sctp/sm.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *,
249249
int, __be16);
250250
struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
251251
union sctp_addr *addr);
252-
int sctp_verify_asconf(const struct sctp_association *asoc,
253-
struct sctp_paramhdr *param_hdr, void *chunk_end,
254-
struct sctp_paramhdr **errp);
252+
bool sctp_verify_asconf(const struct sctp_association *asoc,
253+
struct sctp_chunk *chunk, bool addr_param_needed,
254+
struct sctp_paramhdr **errp);
255255
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
256256
struct sctp_chunk *asconf);
257257
int sctp_process_asconf_ack(struct sctp_association *asoc,

‎net/sctp/sm_make_chunk.c‎

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,50 +3126,63 @@ static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
31263126
return SCTP_ERROR_NO_ERROR;
31273127
}
31283128

3129-
/* Verify the ASCONF packet before we process it. */
3130-
int sctp_verify_asconf(const struct sctp_association *asoc,
3131-
struct sctp_paramhdr *param_hdr, void *chunk_end,
3132-
struct sctp_paramhdr **errp) {
3133-
sctp_addip_param_t *asconf_param;
3129+
/* Verify the ASCONF packet before we process it. */
3130+
bool sctp_verify_asconf(const struct sctp_association *asoc,
3131+
struct sctp_chunk *chunk, bool addr_param_needed,
3132+
struct sctp_paramhdr **errp)
3133+
{
3134+
sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) chunk->chunk_hdr;
31343135
union sctp_params param;
3135-
int length, plen;
3136-
3137-
param.v = (sctp_paramhdr_t *) param_hdr;
3138-
while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
3139-
length = ntohs(param.p->length);
3140-
*errp = param.p;
3136+
bool addr_param_seen = false;
31413137

3142-
if (param.v > chunk_end - length ||
3143-
length < sizeof(sctp_paramhdr_t))
3144-
return 0;
3138+
sctp_walk_params(param, addip, addip_hdr.params) {
3139+
size_t length = ntohs(param.p->length);
31453140

3141+
*errp = param.p;
31463142
switch (param.p->type) {
3143+
case SCTP_PARAM_ERR_CAUSE:
3144+
break;
3145+
case SCTP_PARAM_IPV4_ADDRESS:
3146+
if (length != sizeof(sctp_ipv4addr_param_t))
3147+
return false;
3148+
addr_param_seen = true;
3149+
break;
3150+
case SCTP_PARAM_IPV6_ADDRESS:
3151+
if (length != sizeof(sctp_ipv6addr_param_t))
3152+
return false;
3153+
addr_param_seen = true;
3154+
break;
31473155
case SCTP_PARAM_ADD_IP:
31483156
case SCTP_PARAM_DEL_IP:
31493157
case SCTP_PARAM_SET_PRIMARY:
3150-
asconf_param = (sctp_addip_param_t *)param.v;
3151-
plen = ntohs(asconf_param->param_hdr.length);
3152-
if (plen < sizeof(sctp_addip_param_t) +
3153-
sizeof(sctp_paramhdr_t))
3154-
return 0;
3158+
/* In ASCONF chunks, these need to be first. */
3159+
if (addr_param_needed && !addr_param_seen)
3160+
return false;
3161+
length = ntohs(param.addip->param_hdr.length);
3162+
if (length < sizeof(sctp_addip_param_t) +
3163+
sizeof(sctp_paramhdr_t))
3164+
return false;
31553165
break;
31563166
case SCTP_PARAM_SUCCESS_REPORT:
31573167
case SCTP_PARAM_ADAPTATION_LAYER_IND:
31583168
if (length != sizeof(sctp_addip_param_t))
3159-
return 0;
3160-
3169+
return false;
31613170
break;
31623171
default:
3163-
break;
3172+
/* This is unkown to us, reject! */
3173+
return false;
31643174
}
3165-
3166-
param.v += WORD_ROUND(length);
31673175
}
31683176

3169-
if (param.v != chunk_end)
3170-
return 0;
3177+
/* Remaining sanity checks. */
3178+
if (addr_param_needed && !addr_param_seen)
3179+
return false;
3180+
if (!addr_param_needed && addr_param_seen)
3181+
return false;
3182+
if (param.v != chunk->chunk_end)
3183+
return false;
31713184

3172-
return 1;
3185+
return true;
31733186
}
31743187

31753188
/* Process an incoming ASCONF chunk with the next expected serial no. and
@@ -3178,16 +3191,17 @@ int sctp_verify_asconf(const struct sctp_association *asoc,
31783191
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
31793192
struct sctp_chunk *asconf)
31803193
{
3194+
sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) asconf->chunk_hdr;
3195+
bool all_param_pass = true;
3196+
union sctp_params param;
31813197
sctp_addiphdr_t *hdr;
31823198
union sctp_addr_param *addr_param;
31833199
sctp_addip_param_t *asconf_param;
31843200
struct sctp_chunk *asconf_ack;
3185-
31863201
__be16 err_code;
31873202
int length = 0;
31883203
int chunk_len;
31893204
__u32 serial;
3190-
int all_param_pass = 1;
31913205

31923206
chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
31933207
hdr = (sctp_addiphdr_t *)asconf->skb->data;
@@ -3215,38 +3229,35 @@ struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
32153229
goto done;
32163230

32173231
/* Process the TLVs contained within the ASCONF chunk. */
3218-
while (chunk_len > 0) {
3232+
sctp_walk_params(param, addip, addip_hdr.params) {
3233+
/* Skip preceeding address parameters. */
3234+
if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3235+
param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3236+
continue;
3237+
32193238
err_code = sctp_process_asconf_param(asoc, asconf,
3220-
asconf_param);
3239+
param.addip);
32213240
/* ADDIP 4.1 A7)
32223241
* If an error response is received for a TLV parameter,
32233242
* all TLVs with no response before the failed TLV are
32243243
* considered successful if not reported. All TLVs after
32253244
* the failed response are considered unsuccessful unless
32263245
* a specific success indication is present for the parameter.
32273246
*/
3228-
if (SCTP_ERROR_NO_ERROR != err_code)
3229-
all_param_pass = 0;
3230-
3247+
if (err_code != SCTP_ERROR_NO_ERROR)
3248+
all_param_pass = false;
32313249
if (!all_param_pass)
3232-
sctp_add_asconf_response(asconf_ack,
3233-
asconf_param->crr_id, err_code,
3234-
asconf_param);
3250+
sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3251+
err_code, param.addip);
32353252

32363253
/* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
32373254
* an IP address sends an 'Out of Resource' in its response, it
32383255
* MUST also fail any subsequent add or delete requests bundled
32393256
* in the ASCONF.
32403257
*/
3241-
if (SCTP_ERROR_RSRC_LOW == err_code)
3258+
if (err_code == SCTP_ERROR_RSRC_LOW)
32423259
goto done;
3243-
3244-
/* Move to the next ASCONF param. */
3245-
length = ntohs(asconf_param->param_hdr.length);
3246-
asconf_param = (void *)asconf_param + length;
3247-
chunk_len -= length;
32483260
}
3249-
32503261
done:
32513262
asoc->peer.addip_serial++;
32523263

‎net/sctp/sm_statefuns.c‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,9 +3595,7 @@ sctp_disposition_t sctp_sf_do_asconf(struct net *net,
35953595
struct sctp_chunk *asconf_ack = NULL;
35963596
struct sctp_paramhdr *err_param = NULL;
35973597
sctp_addiphdr_t *hdr;
3598-
union sctp_addr_param *addr_param;
35993598
__u32 serial;
3600-
int length;
36013599

36023600
if (!sctp_vtag_verify(chunk, asoc)) {
36033601
sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
@@ -3622,17 +3620,8 @@ sctp_disposition_t sctp_sf_do_asconf(struct net *net,
36223620
hdr = (sctp_addiphdr_t *)chunk->skb->data;
36233621
serial = ntohl(hdr->serial);
36243622

3625-
addr_param = (union sctp_addr_param *)hdr->params;
3626-
length = ntohs(addr_param->p.length);
3627-
if (length < sizeof(sctp_paramhdr_t))
3628-
return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3629-
(void *)addr_param, commands);
3630-
36313623
/* Verify the ASCONF chunk before processing it. */
3632-
if (!sctp_verify_asconf(asoc,
3633-
(sctp_paramhdr_t *)((void *)addr_param + length),
3634-
(void *)chunk->chunk_end,
3635-
&err_param))
3624+
if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
36363625
return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
36373626
(void *)err_param, commands);
36383627

@@ -3750,10 +3739,7 @@ sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
37503739
rcvd_serial = ntohl(addip_hdr->serial);
37513740

37523741
/* Verify the ASCONF-ACK chunk before processing it. */
3753-
if (!sctp_verify_asconf(asoc,
3754-
(sctp_paramhdr_t *)addip_hdr->params,
3755-
(void *)asconf_ack->chunk_end,
3756-
&err_param))
3742+
if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
37573743
return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
37583744
(void *)err_param, commands);
37593745

0 commit comments

Comments
 (0)