From 94e57496df5f34bfe669eafb9c4af629c04c2713 Mon Sep 17 00:00:00 2001 From: kellyyeh <42761586+kellyyeh@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:18:56 -0800 Subject: [PATCH] [201811][dhcp6relay] a couple memory access protections (#9861) --- src/dhcp6relay/src/relay.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/dhcp6relay/src/relay.cpp b/src/dhcp6relay/src/relay.cpp index da8b5949ca..92efb8d4fd 100644 --- a/src/dhcp6relay/src/relay.cpp +++ b/src/dhcp6relay/src/relay.cpp @@ -225,9 +225,11 @@ void send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n, std::string counterVlan = counter_table; if(sendto(sock, buffer, n, 0, (const struct sockaddr *)&target, sizeof(target)) == -1) syslog(LOG_ERR, "sendto: Failed to send to target address\n"); - else { + else if (counterMap.find(msg_type) != counterMap.end()) { counters[msg_type]++; update_counter(counterVlan.append(config->interface), msg_type); + } else { + syslog(LOG_WARNING, "unexpected message type %d(0x%x)\n", msg_type, msg_type); } } @@ -477,10 +479,9 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h * * @return none */ - void relay_relay_reply(int sock, const uint8_t *msg, int32_t len, relay_config *configs) { + void relay_relay_reply(int sock, const uint8_t *msg, int32_t len, relay_config *config) { static uint8_t buffer[4096]; uint8_t type = 0; - char ifname[configs->interface.size()]; struct sockaddr_in6 target_addr; auto current_buffer_position = buffer; auto current_position = msg; @@ -505,14 +506,13 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h } } - strcpy(ifname, configs->interface.c_str()); memcpy(&target_addr.sin6_addr, &dhcp_relay_header->peer_address, sizeof(struct in6_addr)); target_addr.sin6_family = AF_INET6; target_addr.sin6_flowinfo = 0; target_addr.sin6_port = htons(CLIENT_PORT); - target_addr.sin6_scope_id = if_nametoindex(ifname); + target_addr.sin6_scope_id = if_nametoindex(config->interface.c_str()); - send_udp(sock, buffer, target_addr, current_buffer_position - buffer, configs, type); + send_udp(sock, buffer, target_addr, current_buffer_position - buffer, config, type); } @@ -706,8 +706,7 @@ void loop_relay(std::vector *vlans) { int filter = 0; int local_sock = 0; int server_sock = 0; - const char *ifname = config->interface.c_str(); - int index = if_nametoindex(ifname); + int index = if_nametoindex(config->interface.c_str()); std::string counterVlan = counter_table; initialize_counter(counterVlan.append(config->interface));