From: Roy Marples Date: Wed, 14 May 2008 09:07:54 +0000 (+0000) Subject: If we have space, ensure our packet options are correctly terminated. X-Git-Tag: v4.0.2~414 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52eedd1bc7e1f0cb7eeabd43118d22c86a63401d;p=thirdparty%2Fdhcpcd.git If we have space, ensure our packet options are correctly terminated. --- diff --git a/client.c b/client.c index af0d8169..1227c6b8 100644 --- a/client.c +++ b/client.c @@ -1185,6 +1185,8 @@ handle_packet(struct if_state *state, const struct options *options) { struct interface *iface = state->interface; struct dhcp_message *dhcp; + uint8_t *p; + ssize_t bytes; /* Allocate our buffer space for BPF. * We cannot do this until we have opened our socket as we don't @@ -1199,13 +1201,11 @@ handle_packet(struct if_state *state, const struct options *options) * the first one fails for any reason, we can use the next. */ dhcp = xmalloc(sizeof(*dhcp)); - do { - if (get_packet(iface, (uint8_t *)dhcp, - state->buffer, - &state->buffer_len, &state->buffer_pos) == -1) + bytes = get_packet(iface, (uint8_t *)dhcp, state->buffer, + &state->buffer_len, &state->buffer_pos); + if (bytes == -1) break; - if (dhcp->cookie != htonl(MAGIC_COOKIE)) { logger(LOG_DEBUG, "bogus cookie, ignoring"); continue; @@ -1217,6 +1217,15 @@ handle_packet(struct if_state *state, const struct options *options) dhcp->xid, state->xid); continue; } + /* We should ensure that the packet is terminated correctly + * if we have space for the terminator */ + if ((size_t)bytes < sizeof(struct dhcp_message)) { + p = (uint8_t *)dhcp + bytes - 1; + while (p > dhcp->options && *p == DHCP_PAD) + p--; + if (*p != DHCP_END) + *++p = DHCP_END; + } if (handle_dhcp(state, &dhcp, options) == 0) return 0; } while (state->buffer_pos != 0);