lx_emul: sanitize size 0 for __kmalloc

Driver code such as mfd-core.c may pass 0 as argument n to kcalloc,
which eventually results in an allocation size 0.

  res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);

Since 'res' is checked against NULL for success, kmalloc must not return
a NULL pointer in this case. The patch works around this issue by
forcing an allocation size of 1 byte in this case.

Issue #4253
This commit is contained in:
Norman Feske 2021-08-20 16:38:33 +02:00 committed by Christian Helmuth
parent 29032caf40
commit c95af254f4

View File

@ -32,8 +32,14 @@ void kfree(const void * x)
void * __kmalloc(size_t size, gfp_t flags)
{
/* Linux expects a non-NULL return value for size 0 */
if (size == 0)
size = 1;
/* DMA memory is not implemented yet */
if (flags & GFP_DMA) lx_emul_trace_and_stop(__func__);
if (flags & GFP_DMA)
lx_emul_trace_and_stop(__func__);
return lx_emul_mem_alloc(size);
}