From beea9895e2e66974cfbda242ae0b48777fb3c019 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 17 Mar 2022 16:14:04 +0000 Subject: [PATCH] [dhcp_relay] Check payload size to prevent buffer overflow in dhcpv6 option (#9740) (#10252) --- src/dhcp6relay/src/relay.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/dhcp6relay/src/relay.cpp b/src/dhcp6relay/src/relay.cpp index a2e9fecaa8..c4589774bf 100644 --- a/src/dhcp6relay/src/relay.cpp +++ b/src/dhcp6relay/src/relay.cpp @@ -531,15 +531,18 @@ void relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr * auto position = current_position + sizeof(struct dhcpv6_option); auto dhcpv6msg = parse_dhcpv6_hdr(position); - - while ((current_position - msg) != len) { + + while ((current_position - msg) < len) { auto option = parse_dhcpv6_opt(current_position, &tmp); current_position = tmp; + if (current_position - msg > len || ntohs(option->option_length) > sizeof(buffer) - (current_buffer_position - buffer)) { + break; + } switch (ntohs(option->option_code)) { case OPTION_RELAY_MSG: memcpy(current_buffer_position, ((uint8_t *)option) + sizeof(struct dhcpv6_option), ntohs(option->option_length)); current_buffer_position += ntohs(option->option_length); - type = dhcpv6msg->msg_type;; + type = dhcpv6msg->msg_type; break; default: break;