Skip to content

Commit d4dbef7

Browse files
marceloleitnergregkh
authored andcommitted
sctp: validate from_addr_param return
[ Upstream commit 0c5dc07 ] Ilja reported that, simply putting it, nothing was validating that from_addr_param functions were operating on initialized memory. That is, the parameter itself was being validated by sctp_walk_params, but it doesn't check for types and their specific sizes and it could be a 0-length one, causing from_addr_param to potentially work over the next parameter or even uninitialized memory. The fix here is to, in all calls to from_addr_param, check if enough space is there for the wanted IP address type. Reported-by: Ilja Van Sprundel <ivansprundel@ioactive.com> Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e83f312 commit d4dbef7

6 files changed

Lines changed: 44 additions & 26 deletions

File tree

‎include/net/sctp/structs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ struct sctp_af {
458458
int saddr);
459459
void (*from_sk) (union sctp_addr *,
460460
struct sock *sk);
461-
void (*from_addr_param) (union sctp_addr *,
461+
bool (*from_addr_param) (union sctp_addr *,
462462
union sctp_addr_param *,
463463
__be16 port, int iif);
464464
int (*to_addr_param) (const union sctp_addr *,

‎net/sctp/bind_addr.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,19 @@ int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
270270
rawaddr = (union sctp_addr_param *)raw_addr_list;
271271

272272
af = sctp_get_af_specific(param_type2af(param->type));
273-
if (unlikely(!af)) {
273+
if (unlikely(!af) ||
274+
!af->from_addr_param(&addr, rawaddr, htons(port), 0)) {
274275
retval = -EINVAL;
275-
sctp_bind_addr_clean(bp);
276-
break;
276+
goto out_err;
277277
}
278278

279-
af->from_addr_param(&addr, rawaddr, htons(port), 0);
280279
if (sctp_bind_addr_state(bp, &addr) != -1)
281280
goto next;
282281
retval = sctp_add_bind_addr(bp, &addr, sizeof(addr),
283282
SCTP_ADDR_SRC, gfp);
284-
if (retval) {
283+
if (retval)
285284
/* Can't finish building the list, clean up. */
286-
sctp_bind_addr_clean(bp);
287-
break;
288-
}
285+
goto out_err;
289286

290287
next:
291288
len = ntohs(param->length);
@@ -294,6 +291,12 @@ int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
294291
}
295292

296293
return retval;
294+
295+
out_err:
296+
if (retval)
297+
sctp_bind_addr_clean(bp);
298+
299+
return retval;
297300
}
298301

299302
/********************************************************************

‎net/sctp/input.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,8 @@ static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
11311131
if (!af)
11321132
continue;
11331133

1134-
af->from_addr_param(paddr, params.addr, sh->source, 0);
1134+
if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
1135+
continue;
11351136

11361137
asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
11371138
if (asoc)
@@ -1174,7 +1175,8 @@ static struct sctp_association *__sctp_rcv_asconf_lookup(
11741175
if (unlikely(!af))
11751176
return NULL;
11761177

1177-
af->from_addr_param(&paddr, param, peer_port, 0);
1178+
if (af->from_addr_param(&paddr, param, peer_port, 0))
1179+
return NULL;
11781180

11791181
return __sctp_lookup_association(net, laddr, &paddr, transportp);
11801182
}

‎net/sctp/ipv6.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,20 @@ static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
530530
}
531531

532532
/* Initialize a sctp_addr from an address parameter. */
533-
static void sctp_v6_from_addr_param(union sctp_addr *addr,
533+
static bool sctp_v6_from_addr_param(union sctp_addr *addr,
534534
union sctp_addr_param *param,
535535
__be16 port, int iif)
536536
{
537+
if (ntohs(param->v6.param_hdr.length) < sizeof(struct sctp_ipv6addr_param))
538+
return false;
539+
537540
addr->v6.sin6_family = AF_INET6;
538541
addr->v6.sin6_port = port;
539542
addr->v6.sin6_flowinfo = 0; /* BUG */
540543
addr->v6.sin6_addr = param->v6.addr;
541544
addr->v6.sin6_scope_id = iif;
545+
546+
return true;
542547
}
543548

544549
/* Initialize an address parameter from a sctp_addr and return the length

‎net/sctp/protocol.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,19 @@ static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
253253
}
254254

255255
/* Initialize a sctp_addr from an address parameter. */
256-
static void sctp_v4_from_addr_param(union sctp_addr *addr,
256+
static bool sctp_v4_from_addr_param(union sctp_addr *addr,
257257
union sctp_addr_param *param,
258258
__be16 port, int iif)
259259
{
260+
if (ntohs(param->v4.param_hdr.length) < sizeof(struct sctp_ipv4addr_param))
261+
return false;
262+
260263
addr->v4.sin_family = AF_INET;
261264
addr->v4.sin_port = port;
262265
addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
263266
memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
267+
268+
return true;
264269
}
265270

266271
/* Initialize an address parameter from a sctp_addr and return the length

‎net/sctp/sm_make_chunk.c‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,11 +2329,13 @@ int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
23292329

23302330
/* Process the initialization parameters. */
23312331
sctp_walk_params(param, peer_init, init_hdr.params) {
2332-
if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2333-
param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2332+
if (!src_match &&
2333+
(param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2334+
param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
23342335
af = sctp_get_af_specific(param_type2af(param.p->type));
2335-
af->from_addr_param(&addr, param.addr,
2336-
chunk->sctp_hdr->source, 0);
2336+
if (!af->from_addr_param(&addr, param.addr,
2337+
chunk->sctp_hdr->source, 0))
2338+
continue;
23372339
if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
23382340
src_match = 1;
23392341
}
@@ -2514,7 +2516,8 @@ static int sctp_process_param(struct sctp_association *asoc,
25142516
break;
25152517
do_addr_param:
25162518
af = sctp_get_af_specific(param_type2af(param.p->type));
2517-
af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2519+
if (!af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0))
2520+
break;
25182521
scope = sctp_scope(peer_addr);
25192522
if (sctp_in_scope(net, &addr, scope))
25202523
if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
@@ -2615,15 +2618,13 @@ static int sctp_process_param(struct sctp_association *asoc,
26152618
addr_param = param.v + sizeof(struct sctp_addip_param);
26162619

26172620
af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2618-
if (af == NULL)
2621+
if (!af)
26192622
break;
26202623

2621-
af->from_addr_param(&addr, addr_param,
2622-
htons(asoc->peer.port), 0);
2624+
if (!af->from_addr_param(&addr, addr_param,
2625+
htons(asoc->peer.port), 0))
2626+
break;
26232627

2624-
/* if the address is invalid, we can't process it.
2625-
* XXX: see spec for what to do.
2626-
*/
26272628
if (!af->addr_valid(&addr, NULL, NULL))
26282629
break;
26292630

@@ -3037,7 +3038,8 @@ static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
30373038
if (unlikely(!af))
30383039
return SCTP_ERROR_DNS_FAILED;
30393040

3040-
af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
3041+
if (!af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0))
3042+
return SCTP_ERROR_DNS_FAILED;
30413043

30423044
/* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
30433045
* or multicast address.
@@ -3314,7 +3316,8 @@ static void sctp_asconf_param_success(struct sctp_association *asoc,
33143316

33153317
/* We have checked the packet before, so we do not check again. */
33163318
af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3317-
af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3319+
if (!af->from_addr_param(&addr, addr_param, htons(bp->port), 0))
3320+
return;
33183321

33193322
switch (asconf_param->param_hdr.type) {
33203323
case SCTP_PARAM_ADD_IP:

0 commit comments

Comments
 (0)