ldpd(8): Fix address of misaligned packed members.

PR kern/56895
This commit is contained in:
riastradh 2022-06-26 17:55:38 +00:00
parent a27b16dfba
commit 7feed94cb3
4 changed files with 23 additions and 17 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.7 2021/02/23 15:05:08 joerg Exp $
# $NetBSD: Makefile,v 1.8 2022/06/26 17:55:38 riastradh Exp $
.include <bsd.own.mk>
@ -26,7 +26,4 @@ LDADD+= -lcrypt
CPPFLAGS+=-DINET6
.endif
CWARNFLAGS.clang+= -Wno-error=address-of-packed-member
CWARNFLAGS.gcc+= ${GCC_NO_ADDR_OF_PACKED_MEMBER}
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: fsm.c,v 1.15 2014/03/18 18:20:47 riastradh Exp $ */
/* $NetBSD: fsm.c,v 1.16 2022/06/26 17:55:38 riastradh Exp $ */
/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
@ -59,6 +59,7 @@ run_ldp_hello(const struct ldp_pdu * pduid, const struct hello_tlv * ht,
const struct transport_address_tlv *trtlv;
struct hello_info *hi = NULL;
union sockunion traddr;
struct in_addr ldp_id;
if ((!pduid) || (!ht))
return;
@ -125,7 +126,8 @@ run_ldp_hello(const struct ldp_pdu * pduid, const struct hello_tlv * ht,
hi->keepalive = LDP_THELLO_KEEP;
}
if (!get_ldp_peer_by_id(&pduid->ldp_id)) {
ldp_id = pduid->ldp_id;
if (!get_ldp_peer_by_id(&ldp_id)) {
/*
* RFC 5036 2.5.2: If A1 > A2, LSR1 plays the active role;
* otherwise it is passive.
@ -134,7 +136,7 @@ run_ldp_hello(const struct ldp_pdu * pduid, const struct hello_tlv * ht,
(hi->transport_address.sa.sa_family == AF_INET &&
ntohl(hi->transport_address.sin.sin_addr.s_addr) <
ntohl(ladd->s_addr))) {
peer = ldp_peer_new(&pduid->ldp_id, padd,
peer = ldp_peer_new(&ldp_id, padd,
&hi->transport_address.sa,
ntohs(ht->ch.holdtime), 0);
if (peer == NULL)
@ -151,7 +153,7 @@ build_address_list_tlv(void)
struct address_list_tlv *t;
struct ifaddrs *ifa, *ifb;
struct sockaddr_in *sa;
struct in_addr *ia;
char *ia;
uint16_t adrcount = 0;
if (getifaddrs(&ifa) == -1)
@ -184,7 +186,7 @@ build_address_list_tlv(void)
adrcount * sizeof(struct in_addr));
t->a_af = htons(LDP_AF_INET);
ia = &t->a_address;
ia = (void *)&t->a_address;
for (adrcount = 0, ifb = ifa; ifb; ifb = ifb->ifa_next) {
if ((ifb->ifa_addr->sa_family != AF_INET) ||
(!(ifb->ifa_flags & IFF_UP)))
@ -192,7 +194,8 @@ build_address_list_tlv(void)
sa = (struct sockaddr_in *) ifb->ifa_addr;
if (ntohl(sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET)
continue;
memcpy(&ia[adrcount], &sa->sin_addr, sizeof(struct in_addr));
memcpy(ia + adrcount*sizeof(struct in_addr), &sa->sin_addr,
sizeof(struct in_addr));
adrcount++;
}
freeifaddrs(ifa);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ldp_peer.c,v 1.18 2020/06/22 07:50:53 msaitoh Exp $ */
/* $NetBSD: ldp_peer.c,v 1.19 2022/06/26 17:55:38 riastradh Exp $ */
/*
* Copyright (c) 2010 The NetBSD Foundation, Inc.
@ -307,7 +307,7 @@ int
add_ifaddresses(struct ldp_peer * p, const struct al_tlv * a)
{
int i, c, n;
const struct in_addr *ia;
const char *ia;
struct sockaddr_in ipa;
memset(&ipa, 0, sizeof(ipa));
@ -329,8 +329,9 @@ add_ifaddresses(struct ldp_peer * p, const struct al_tlv * a)
debugp("Trying to add %d addresses to peer %s ... \n", n,
inet_ntoa(p->ldp_id));
for (ia = (const struct in_addr *) & a->address,c = 0,i = 0; i<n; i++) {
memcpy(&ipa.sin_addr, &ia[i], sizeof(ipa.sin_addr));
for (ia = (const void *)&a->address, c = 0, i = 0; i < n; i++) {
memcpy(&ipa.sin_addr, ia + i*sizeof(ipa.sin_addr),
sizeof(ipa.sin_addr));
if (add_ifaddr(p, (struct sockaddr *)&ipa) == LDP_E_OK)
c++;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: socketops.c,v 1.35 2020/04/22 23:53:27 joerg Exp $ */
/* $NetBSD: socketops.c,v 1.36 2022/06/26 17:55:38 riastradh Exp $ */
/*
* Copyright (c) 2010 The NetBSD Foundation, Inc.
@ -404,6 +404,7 @@ send_hello(void)
struct hello_tlv *t;
struct common_hello_tlv *cht;
struct ldp_pdu *spdu;
struct in_addr ldp_id;
struct transport_address_tlv *trtlv;
void *v;
struct sockaddr_in sadest; /* Destination ALL_ROUTERS */
@ -443,7 +444,8 @@ send_hello(void)
/* Prepare PDU envelope */
spdu->version = htons(LDP_VERSION);
spdu->length = htons(IPV4_HELLO_MSG_SIZE - PDU_VER_LENGTH);
inet_aton(LDP_ID, &spdu->ldp_id);
inet_aton(LDP_ID, &ldp_id);
spdu->ldp_id = ldp_id;
/* Prepare Hello TLV */
t->type = htons(LDP_HELLO);
@ -1387,10 +1389,13 @@ send_message(const struct ldp_peer * p, const struct ldp_pdu * pdu,
int
send_tlv(const struct ldp_peer * p, const struct tlv * t)
{
struct in_addr ldp_id;
struct ldp_pdu pdu;
inet_aton(LDP_ID, &ldp_id);
pdu.version = htons(LDP_VERSION);
inet_aton(LDP_ID, &pdu.ldp_id);
pdu.ldp_id = ldp_id;
pdu.label_space = 0;
pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH +
PDU_PAYLOAD_LENGTH);