security: fix integer overflow in pw_reallocarray fallback path

Memory Safety: High

When the system does not provide reallocarray(), pw_reallocarray()
falls back to realloc(ptr, nmemb * size). The multiplication
nmemb * size can silently overflow, causing a smaller-than-expected
allocation. Subsequent writes to the allocation then overflow the
heap buffer.

This function is used extensively throughout PipeWire for allocating
arrays from protocol data, making it a wide attack surface.

Fix by adding an explicit overflow check before the multiplication
in the fallback path, matching the behavior of the real
reallocarray().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Wim Taymans 2026-04-23 17:57:22 +02:00
parent d4ec630b2f
commit 18df76b654

View file

@ -368,6 +368,10 @@ void* pw_reallocarray(void *ptr, size_t nmemb, size_t size)
#ifdef HAVE_REALLOCARRAY
return reallocarray(ptr, nmemb, size);
#else
if (size > 0 && nmemb > SIZE_MAX / size) {
errno = ENOMEM;
return NULL;
}
return realloc(ptr, nmemb * size);
#endif
}