Skip to content

Commit

Permalink
MAC address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmj committed Sep 17, 2024
1 parent 71ed8ff commit f6c6af1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/hypervisor_docker.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <linux/if_bridge.h>
#include <sched.h>
#include <linux/ethtool.h>
#include <regex.h>

#include "ubridge.h"
#include "hypervisor.h"
Expand Down Expand Up @@ -313,14 +314,23 @@ static int cmd_set_mac_addr(hypervisor_conn_t *conn, int argc, char *argv[])
int fd;
struct ifreq ifr;
char *interface = argv[0];
char mac_char[17];
char mac_char[18];
regex_t regex;

if (strlen(argv[1]) != 17) {
if (regcomp(&regex, "^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$", REG_EXTENDED)) {
hypervisor_send_reply(conn, HSC_ERR_CREATE, 1, "could not compile regex");
return (-1);
}

if (regexec(&regex, argv[1], 0, NULL, 0)) {
hypervisor_send_reply(conn, HSC_ERR_CREATE, 1, "invalid MAC address format");
regfree(&regex);
return (-1);
}
regfree(&regex);

strncpy(mac_char, argv[1], 17);
mac_char[17] = '\0';
sscanf(mac_char, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&ifr.ifr_hwaddr.sa_data[0],
&ifr.ifr_hwaddr.sa_data[1],
Expand All @@ -335,7 +345,8 @@ static int cmd_set_mac_addr(hypervisor_conn_t *conn, int argc, char *argv[])
return (-1);
}

strncpy(ifr.ifr_name, interface, strlen(interface));
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;

if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
Expand Down

0 comments on commit f6c6af1

Please sign in to comment.