NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/41682: dhcpcd Segmentation Fault on NetBSD/sgimips 5.0 (patch included)



>Number:         41682
>Category:       bin
>Synopsis:       dhcpcd Segmentation Fault on NetBSD/sgimips 5.0 (patch 
>included)
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jul 08 06:55:00 +0000 2009
>Originator:     Timothy McIntosh
>Release:        NetBSD/sgimips 5.0
>Organization:
Green Dome Software
>Environment:
NetBSD hoth.astro.net 5.0 NetBSD 5.0 (GENERIC32_IP2x) #0: Mon Apr 27 06:08:08 
UTC 2009  
builds%b1.netbsd.org@localhost:/home/builds/ab/netbsd-5-0-RELEASE/sgimips/200904260229Z-obj/home/builds/ab/netbsd-5-0-RELEASE/src/sys/arch/sgimips/compile/GENERIC32_IP2x
 sgimips

>Description:
The command /sbin/dhcpcd/dhcpcd -n -B -E sq1 crashes with a segmentation fault.

The problem is caused by passing an IPv4 address (in_addr_t) from an instance 
of 'struct in_addr' to the function dhcp.c:get_option_addr() as type 
(uint32_t).  In NetBSD, struct in_addr is defined as a packed structure.  The 
result is that an instance of struct in_addr may not be aligned to a 32-bit 
address boundary, which is what happens with the variable 'net' in 
dhcp.c:configure_env() on sgimips at -O2.  Since get_option_addr() is declared 
to take a (presumably aligned) pointer to uint32_t, at -O2 the memcpy() in 
get_option_addr() at dhcp.c:336 is replaced with a simple store word 
instruction.  This results in a SIGSEGV (presumably due to an alignment 
exception) when this function is called from configure_env() at dhcp.c:1234.

Reference:
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Type-Attributes.html#Type-Attributes
>How-To-Repeat:
% /sbin/dhcpcd/dhcpcd -n -B -E sq1
sq1: dhcpcd 4.0.10 starting
sq1: broadcasting for a lease
sq1: offered [...] from [...]
sq1: acknowledged [...] from [...]
sq1: checking [...] is available on attached networks
sq1: leased [...] for 341521 seconds
Segmentation fault
%

>Fix:
The function get_option_addr() in dhcp.c should be changed to accept a pointer 
to 'struct in_addr' rather than a pointer to uint32_t.  All calls to this 
function must be adjusted accordingly.  The attached patch fixes the problem on 
my system.

Index: dist/client.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/Attic/client.c,v
retrieving revision 1.1.1.2.6.2
diff -u -r1.1.1.2.6.2 client.c
--- dist/client.c       6 Feb 2009 02:25:38 -0000       1.1.1.2.6.2
+++ dist/client.c       8 Jul 2009 06:41:36 -0000
@@ -368,7 +368,7 @@
                return;
        lease->addr.s_addr = dhcp->yiaddr;
 
-       if (get_option_addr(&lease->net.s_addr, dhcp, DHO_SUBNETMASK) == -1)
+       if (get_option_addr(&lease->net, dhcp, DHO_SUBNETMASK) == -1)
                lease->net.s_addr = get_netmask(dhcp->yiaddr);
        if (get_option_uint32(&lease->leasetime, dhcp, DHO_LEASETIME) == 0) {
                /* Ensure that we can use the lease */
@@ -1346,7 +1346,7 @@
                addr.s_addr = dhcp->yiaddr;
                a = xstrdup(inet_ntoa(addr));
        }
-       r = get_option_addr(&addr.s_addr, dhcp, DHO_SERVERID);
+       r = get_option_addr(&addr, dhcp, DHO_SERVERID);
        if (dhcp->servername[0] && r == 0)
                logger(lvl, "%s %s from %s `%s'", msg, a,
                       inet_ntoa(addr), dhcp->servername);
@@ -1382,7 +1382,7 @@
         * We should expand this to check IP and/or hardware address
         * at the packet level. */
        if (options->blacklist_len != 0 &&
-           get_option_addr(&addr.s_addr, dhcp, DHO_SERVERID) == 0)
+           get_option_addr(&addr, dhcp, DHO_SERVERID) == 0)
        {
                for (i = 0; i < options->blacklist_len; i++) {
                        if (options->blacklist[i] != addr.s_addr)
@@ -1434,7 +1434,7 @@
 
        if (type == DHCP_OFFER && state->state == STATE_DISCOVERING) {
                lease->addr.s_addr = dhcp->yiaddr;
-               get_option_addr(&lease->server.s_addr, dhcp, DHO_SERVERID);
+               get_option_addr(&lease->server, dhcp, DHO_SERVERID);
                log_dhcp(LOG_INFO, "offered", dhcp);
                if (state->options & DHCPCD_TEST) {
                        run_script(options, iface->name, "TEST", dhcp, NULL);
@@ -1467,7 +1467,7 @@
        case STATE_RENEWING:
        case STATE_REBINDING:
                if (!(state->options & DHCPCD_INFORM)) {
-                       get_option_addr(&lease->server.s_addr,
+                       get_option_addr(&lease->server,
                                        dhcp, DHO_SERVERID);
                        log_dhcp(LOG_INFO, "acknowledged", dhcp);
                }
Index: dist/configure.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/configure.c,v
retrieving revision 1.1.1.2.6.1
diff -u -r1.1.1.2.6.1 configure.c
--- dist/configure.c    9 Jan 2009 03:13:49 -0000       1.1.1.2.6.1
+++ dist/configure.c    8 Jul 2009 06:41:37 -0000
@@ -357,9 +357,9 @@
                if (addr.s_addr == 0)
                        addr.s_addr = lease->addr.s_addr;
                /* Ensure we have all the needed values */
-               if (get_option_addr(&net.s_addr, dhcp, DHO_SUBNETMASK) == -1)
+               if (get_option_addr(&net, dhcp, DHO_SUBNETMASK) == -1)
                        net.s_addr = get_netmask(addr.s_addr);
-               if (get_option_addr(&brd.s_addr, dhcp, DHO_BROADCAST) == -1)
+               if (get_option_addr(&brd, dhcp, DHO_BROADCAST) == -1)
                        brd.s_addr = addr.s_addr | ~net.s_addr;
        }
 
Index: dist/dhcp.c
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/dhcp.c,v
retrieving revision 1.1.1.2.6.2
diff -u -r1.1.1.2.6.2 dhcp.c
--- dist/dhcp.c 6 Feb 2009 02:25:38 -0000       1.1.1.2.6.2
+++ dist/dhcp.c 8 Jul 2009 06:41:37 -0000
@@ -327,25 +327,25 @@
 }
 
 int
-get_option_addr(uint32_t *a, const struct dhcp_message *dhcp, uint8_t option)
+get_option_addr(struct in_addr *a, const struct dhcp_message *dhcp, uint8_t 
option)
 {
        const uint8_t *p = get_option_raw(dhcp, option);
 
        if (!p)
                return -1;
-       memcpy(a, p, sizeof(*a));
+       memcpy(&a->s_addr, p, sizeof(*a));
        return 0;
 }
 
 int
 get_option_uint32(uint32_t *i, const struct dhcp_message *dhcp, uint8_t option)
 {
-       uint32_t a;
+       struct in_addr a;
 
        if (get_option_addr(&a, dhcp, option) == -1)
                return -1;
 
-       *i = ntohl(a);
+       *i = ntohl(a.s_addr);
        return 0;
 }
 
@@ -1231,14 +1231,14 @@
                 * message but are not necessarily in the options */
                addr.s_addr = dhcp->yiaddr;
                setvar(&ep, prefix, "ip_address", inet_ntoa(addr));
-               if (get_option_addr(&net.s_addr, dhcp, DHO_SUBNETMASK) == -1) {
+               if (get_option_addr(&net, dhcp, DHO_SUBNETMASK) == -1) {
                        net.s_addr = get_netmask(addr.s_addr);
                        setvar(&ep, prefix, "subnet_mask", inet_ntoa(net));
                }
                i = inet_ntocidr(net);
                snprintf(cidr, sizeof(cidr), "%d", inet_ntocidr(net));
                setvar(&ep, prefix, "subnet_cidr", cidr);
-               if (get_option_addr(&brd.s_addr, dhcp, DHO_BROADCAST) == -1) {
+               if (get_option_addr(&brd, dhcp, DHO_BROADCAST) == -1) {
                        brd.s_addr = addr.s_addr | ~net.s_addr;
                        setvar(&ep, prefix, "broadcast_address", 
inet_ntoa(brd));
                }
Index: dist/dhcp.h
===================================================================
RCS file: /cvsroot/src/external/bsd/dhcpcd/dist/dhcp.h,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 dhcp.h
--- dist/dhcp.h 19 Sep 2008 22:59:58 -0000      1.1.1.2
+++ dist/dhcp.h 8 Jul 2009 06:41:37 -0000
@@ -160,7 +160,7 @@
 int make_option_mask(uint8_t *, char **, int);
 void print_options(void);
 char *get_option_string(const struct dhcp_message *, uint8_t);
-int get_option_addr(uint32_t *, const struct dhcp_message *, uint8_t);
+int get_option_addr(struct in_addr *, const struct dhcp_message *, uint8_t);
 int get_option_uint32(uint32_t *, const struct dhcp_message *, uint8_t);
 int get_option_uint16(uint16_t *, const struct dhcp_message *, uint8_t);
 int get_option_uint8(uint8_t *, const struct dhcp_message *, uint8_t);



Home | Main Index | Thread Index | Old Index