From 67f718f83e50934bb37abc9fb7d9cb9a0488b833 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:19:15 -0800 Subject: [PATCH] [dhcp_relay] Check payload size to prevent buffer overflow in dhcpv6 option (#9740) --- src/dhcp6relay/src/relay.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dhcp6relay/src/relay.cpp b/src/dhcp6relay/src/relay.cpp index c6aa6fac2b..e423f5f9fe 100644 --- a/src/dhcp6relay/src/relay.cpp +++ b/src/dhcp6relay/src/relay.cpp @@ -493,14 +493,17 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h 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;