Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure memcpy of mac lengths does not overflow buffers #18757

Merged
merged 4 commits into from
May 25, 2022
Merged
Changes from 2 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 src/platform/Linux/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,25 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
CHIP_ERROR error = CHIP_NO_ERROR;
bool found = false;

// TODO: ideally the buffer size should have been passed as a span, however
// for now use the size that is validated in GenericConfigurationManagerImpl.ipp
constexpr size_t kExpectedBufMinSize = ConfigurationManager::kPrimaryMACAddressLength;
memset(buf, 0, kExpectedBufMinSize);

VerifyOrExit(getifaddrs(&addresses) == 0, error = CHIP_ERROR_INTERNAL);
for (auto addr = addresses; addr != nullptr; addr = addr->ifa_next)
{
if ((addr->ifa_addr) && (addr->ifa_addr->sa_family == AF_PACKET) && strncmp(addr->ifa_name, "lo", IFNAMSIZ) != 0)
{
struct sockaddr_ll * mac = (struct sockaddr_ll *) addr->ifa_addr;
memcpy(buf, mac->sll_addr, mac->sll_halen);

size_t mac_len = mac->sll_halen;
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
if (mac_len > kExpectedBufMinSize)
{
mac_len = kExpectedBufMinSize;
}

memcpy(buf, mac->sll_addr, mac_len);
found = true;
break;
}
Expand Down