fix: replace strcpy with strncpy

Replace all instances of strcpy() with strncpy() to prevent buffer
overflow vulnerabilities. The strcpy() function does not perform
bounds checking, which can lead to security issues when copying
strings of unknown or excessive length.

Problem reported and fix suggested by Jaeyeong Lee.

Change-Id: Ide896dbae3bdaf9001287b4d3e387efd355e73bd
This commit is contained in:
Hans-Erik Floryd
2025-12-22 13:49:37 +01:00
parent a7c74cea13
commit bb63f23ea7
3 changed files with 8 additions and 4 deletions

View File

@@ -174,7 +174,8 @@ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary)
}
/* connect bpf to NIC by name */
strcpy(ifr.ifr_name, ifname);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (ioctl(*bpf, BIOCSETIF, &ifr) == -1)
{
perror("BIOCSETIF");

View File

@@ -152,10 +152,12 @@ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary)
i = 1;
r |= setsockopt(*psock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i));
/* connect socket to NIC by name */
strcpy(ifr.ifr_name, ifname);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
r |= ioctl(*psock, SIOCGIFINDEX, &ifr);
ifindex = ifr.ifr_ifindex;
strcpy(ifr.ifr_name, ifname);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
ifr.ifr_flags = 0;
/* reset flags of NIC interface */
r |= ioctl(*psock, SIOCGIFFLAGS, &ifr);

View File

@@ -722,7 +722,8 @@ int main(int argc, char *argv[])
if ((argc > 2) && (strncmp(argv[2], "-sdo", sizeof("-sdo")) == 0)) printSDO = TRUE;
if ((argc > 2) && (strncmp(argv[2], "-map", sizeof("-map")) == 0)) printMAP = TRUE;
/* start slaveinfo */
strcpy(ifbuf, argv[1]);
strncpy(ifbuf, argv[1], sizeof(ifbuf) - 1);
ifbuf[sizeof(ifbuf) - 1] = '\0';
slaveinfo(ifbuf);
}
else