NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: bin/45661: Overlapping buffer in route.c
The following reply was made to PR bin/45661; it has been noted by GNATS.
From: Christian Biere <christianbiere%gmx.de@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc:
Subject: Re: bin/45661: Overlapping buffer in route.c
Date: Sun, 27 Nov 2011 14:02:13 +0100
Unless I'm missing something the provided patch is incorrect
because "len" is never updated inside the loop. However, I
suggest using strlcat() instead to avoid pointer arithmetics
with the destination buffer.
There's also a similar issue in routename() which uses
strlcpy() with overlapping memory.
While we're at it, I also suggest fixing the alignment issues
in three places.
--- route.c.orig 2011-11-27 13:09:13.328002503 +0100
+++ route.c 2011-11-27 13:57:01.928002817 +0100
@@ -49,6 +49,7 @@ __RCSID("$NetBSD: route.c,v 1.134 2011/1
#include <sys/ioctl.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
+#include <sys/endian.h>
#include <net/if.h>
#include <net/route.h>
@@ -484,7 +485,7 @@ routename(const struct sockaddr *sa, str
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = strchr(domain, '.')))
- (void)strlcpy(domain, cp + 1, sizeof(domain));
+ memmove(domain, &cp[1], strlen(cp) + 1);
else
domain[0] = 0;
}
@@ -550,7 +551,7 @@ routename(const struct sockaddr *sa, str
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
sin6.sin6_scope_id == 0) {
sin6.sin6_scope_id =
- ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
+ be16dec(&sin6.sin6_addr.s6_addr[2]);
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
}
@@ -593,17 +594,19 @@ routename(const struct sockaddr *sa, str
union mpls_shim ms;
const union mpls_shim *pms;
int psize = sizeof(struct sockaddr_mpls);
+ char label[32];
ms.s_addr =((const struct sockaddr_mpls*)sa)->smpls_addr.s_addr;
ms.s_addr = ntohl(ms.s_addr);
- snprintf(line, sizeof(line), "%u", ms.shim.label);
+ line[0] = '\0';
+ snprintf(label, sizeof label, "%u", ms.shim.label);
pms = &((const struct sockaddr_mpls*)sa)->smpls_addr;
while(psize < sa->sa_len) {
pms++;
ms.s_addr = ntohl(pms->s_addr);
- snprintf(line, sizeof(line), "%s %u", line,
- ms.shim.label);
+ snprintf(label, sizeof label, " %u", ms.shim.label);
+ strlcat(line, sizeof line, label);
psize += sizeof(ms);
}
break;
@@ -716,7 +719,7 @@ netname(const struct sockaddr *sa, struc
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
sin6.sin6_scope_id == 0) {
sin6.sin6_scope_id =
- ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
+ be16dec(&sin6.sin6_addr.s6_addr[2]);
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
}
@@ -1270,8 +1273,8 @@ getaddr(int which, const char *s, struct
if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr)) &&
su->sin6.sin6_scope_id) {
- *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
- htons(su->sin6.sin6_scope_id);
+ be16enc(&su->sin6.sin6_addr.s6_addr[2],
+ su->sin6.sin6_scope_id);
su->sin6.sin6_scope_id = 0;
}
#endif
Home |
Main Index |
Thread Index |
Old Index