Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/everest/io/src/can/socket_can_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
}

bool socket_can_handler::open(std::string const& can_device) {
// IFNAMSIZ is the size of the buffer to write the name to.
// This situation is special concerning null termination,
// The name can occupy the fill buffer. If it does not, nulltermination is necessary
// Since most Linux systems enforce 15 chars as limit plus 1 for nulltermination
// we do the same thing here.
if (can_device.size() >= IFNAMSIZ) {
return false;
}
m_can_dev = can_device;
return open_device() == 0;
}
Expand All @@ -95,7 +103,11 @@
return errno;
}
struct ifreq ifr;
strcpy(ifr.ifr_name, m_can_dev.c_str());
memset(&ifr, 0, sizeof(ifr));
// We know m_can_dev fits because of the check in open().
// strncpy will copy the string and the null terminator.
// The previous memset handles any trailing bytes in the 16-byte buffer.
strncpy(ifr.ifr_name, m_can_dev.c_str(), IFNAMSIZ);

Check failure on line 110 in lib/everest/io/src/can/socket_can_handler.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/everest/io/src/can/socket_can_handler.cpp#L110

Easily used incorrectly; doesn't always \0-terminate or check for invalid pointers [MS-banned] (CWE-120).
if (ioctl(can_fd, SIOCGIFINDEX, &ifr) < 0) {
perror(m_can_dev.c_str());
return errno;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,43 @@
}

bool CanDevice::open_device(const char* dev) {
if (!dev || std::strlen(dev) >= IFNAMSIZ) {

Check failure on line 32 in modules/HardwareDrivers/PowerSupplies/InfyPower_BEG1K075G/can_driver_acdc/CanDevice.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/HardwareDrivers/PowerSupplies/InfyPower_BEG1K075G/can_driver_acdc/CanDevice.cpp#L32

Does not handle strings that are not \0-terminated; if given one it may perform an over-read (it could cause a crash if unprotected) (CWE-126).
fprintf(stderr, "Interface name is invalid or too long: %s\n", dev ? dev : "NULL");
return false;
}

if ((can_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Socket");
return false;
}

} else {

// retrieve interface index from interface name
struct ifreq ifr;
strcpy(ifr.ifr_name, dev);
if (ioctl(can_fd, SIOCGIFINDEX, &ifr) < 0) {
perror(dev);
close(can_fd);
return false;
}
// retrieve interface index from interface name
struct ifreq ifr;
std::memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);

Check failure on line 45 in modules/HardwareDrivers/PowerSupplies/InfyPower_BEG1K075G/can_driver_acdc/CanDevice.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/HardwareDrivers/PowerSupplies/InfyPower_BEG1K075G/can_driver_acdc/CanDevice.cpp#L45

Easily used incorrectly; doesn't always \0-terminate or check for invalid pointers [MS-banned] (CWE-120).
if (ioctl(can_fd, SIOCGIFINDEX, &ifr) < 0) {
perror(dev);
close(can_fd);
return false;
}

// bind to the interface
struct sockaddr_can addr;
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
// bind to the interface
struct sockaddr_can addr;
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(can_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Bind");
close(can_fd);
return false;
}
if (bind(can_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Bind");
close(can_fd);
return false;
}

// spawn read thread
exit_rx_thread = false;
rx_thread_handle = std::thread(&CanDevice::rx_thread, this);
// spawn read thread
exit_rx_thread = false;
rx_thread_handle = std::thread(&CanDevice::rx_thread, this);

return true;
}
return true;
}

bool CanDevice::close_device() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,43 @@
}

bool CanDevice::open_device(const char* dev) {
if (!dev || std::strlen(dev) >= IFNAMSIZ) {

Check failure on line 32 in modules/HardwareDrivers/PowerSupplies/UUGreenPower_UR1000X0/can_driver_acdc/CanDevice.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/HardwareDrivers/PowerSupplies/UUGreenPower_UR1000X0/can_driver_acdc/CanDevice.cpp#L32

Does not handle strings that are not \0-terminated; if given one it may perform an over-read (it could cause a crash if unprotected) (CWE-126).
fprintf(stderr, "Interface name is invalid or too long: %s\n", dev ? dev : "NULL");
return false;
}

if ((can_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Socket");
return false;
}

} else {

// retrieve interface index from interface name
struct ifreq ifr;
strcpy(ifr.ifr_name, dev);
if (ioctl(can_fd, SIOCGIFINDEX, &ifr) < 0) {
perror(dev);
close(can_fd);
return false;
}
// retrieve interface index from interface name
struct ifreq ifr;
std::memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);

Check failure on line 45 in modules/HardwareDrivers/PowerSupplies/UUGreenPower_UR1000X0/can_driver_acdc/CanDevice.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/HardwareDrivers/PowerSupplies/UUGreenPower_UR1000X0/can_driver_acdc/CanDevice.cpp#L45

Easily used incorrectly; doesn't always \0-terminate or check for invalid pointers [MS-banned] (CWE-120).
if (ioctl(can_fd, SIOCGIFINDEX, &ifr) < 0) {
perror(dev);
close(can_fd);
return false;
}

// bind to the interface
struct sockaddr_can addr;
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
// bind to the interface
struct sockaddr_can addr;
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(can_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Bind");
close(can_fd);
return false;
}
if (bind(can_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Bind");
close(can_fd);
return false;
}

// spawn read thread
exit_rx_thread = false;
rx_thread_handle = std::thread(&CanDevice::rx_thread, this);
// spawn read thread
exit_rx_thread = false;
rx_thread_handle = std::thread(&CanDevice::rx_thread, this);

return true;
}
return true;
}

bool CanDevice::close_device() {
Expand Down
Loading