Module Name:    src
Committed By:   riastradh
Date:           Sun Jun 26 17:55:38 UTC 2022

Modified Files:
        src/usr.sbin/ldpd: Makefile fsm.c ldp_peer.c socketops.c

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

PR kern/56895


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.sbin/ldpd/Makefile
cvs rdiff -u -r1.15 -r1.16 src/usr.sbin/ldpd/fsm.c
cvs rdiff -u -r1.18 -r1.19 src/usr.sbin/ldpd/ldp_peer.c
cvs rdiff -u -r1.35 -r1.36 src/usr.sbin/ldpd/socketops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ldpd/Makefile
diff -u src/usr.sbin/ldpd/Makefile:1.7 src/usr.sbin/ldpd/Makefile:1.8
--- src/usr.sbin/ldpd/Makefile:1.7	Tue Feb 23 15:05:08 2021
+++ src/usr.sbin/ldpd/Makefile	Sun Jun 26 17:55:38 2022
@@ -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>

Index: src/usr.sbin/ldpd/fsm.c
diff -u src/usr.sbin/ldpd/fsm.c:1.15 src/usr.sbin/ldpd/fsm.c:1.16
--- src/usr.sbin/ldpd/fsm.c:1.15	Tue Mar 18 18:20:47 2014
+++ src/usr.sbin/ldpd/fsm.c	Sun Jun 26 17:55:38 2022
@@ -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 * pdu
 	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 * pdu
 			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 * pdu
 		    (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);

Index: src/usr.sbin/ldpd/ldp_peer.c
diff -u src/usr.sbin/ldpd/ldp_peer.c:1.18 src/usr.sbin/ldpd/ldp_peer.c:1.19
--- src/usr.sbin/ldpd/ldp_peer.c:1.18	Mon Jun 22 07:50:53 2020
+++ src/usr.sbin/ldpd/ldp_peer.c	Sun Jun 26 17:55:38 2022
@@ -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, con
 	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++;
 	}

Index: src/usr.sbin/ldpd/socketops.c
diff -u src/usr.sbin/ldpd/socketops.c:1.35 src/usr.sbin/ldpd/socketops.c:1.36
--- src/usr.sbin/ldpd/socketops.c:1.35	Wed Apr 22 23:53:27 2020
+++ src/usr.sbin/ldpd/socketops.c	Sun Jun 26 17:55:38 2022
@@ -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, 
 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);

Reply via email to