Systemd/src/pstore
Zbigniew Jędrzejewski-Szmek 8198c3e42b pstore: rework memory handling for dmesg
Semmle Security Reports report:
> The problem occurs on the way realloc is being used. When a size
> bigger than the chunk that wants to be reallocated is passed, realloc
> try to malloc a  bigger size, however in the case that malloc fails
> (for example, by forcing a big allocation)  realloc will return NULL.
>
> According to the man page:
> "The realloc() function returns a pointer to the newly allocated
> memory, which is suitably aligned for any built-in type and may be
> different from ptr,  or  NULL  if  the  request fails.   If size was
> equal to 0, either NULL or a pointer suitable to be passed to free()
> is returned.  If realloc() fails, the original block is left
> untouched; it is  not  freed or moved."
>
> The problem occurs when the memory ptr passed to the first argument of
> realloc is the same as the one used for the result, for example in
> this case:
>
> dmesg = realloc(dmesg, dmesg_size + strlen(pe->dirent.d_name) +
> strlen(":\n") + pe->content_size + 1);
>
> f8bcb81955/files/src/pstore/pstore.c?sort=name&dir=A
> SC&mode=heatmap#L300
>
> If the malloc inside that realloc fails, then the original memory
> chunk will never be free but since realloc will return NULL, the
> pointer to that memory chunk will be lost and a memory leak will
> occur.
>
> In case you are curious, this is the query we used to find this problem:
> https://lgtm.com/query/8650323308193591473/

Let's use a more standard pattern: allocate memory using greedy_realloc, and
instead of freeing it when we wrote out a chunk, let's just move the cursor
back to the beginning and reuse the memory we allocated previously.

If we fail to allocate the memory for dmesg contents, don't write the dmesg
entry, but let's still process the files to move them out of pstore.
2019-10-04 16:21:32 +02:00
..
meson.build pstore: Tool to archive contents of pstore 2019-07-19 21:46:07 +02:00
pstore.c pstore: rework memory handling for dmesg 2019-10-04 16:21:32 +02:00
pstore.conf pstore: Tool to archive contents of pstore 2019-07-19 21:46:07 +02:00