bugAVR C Runtime Library - Bugs: bug #53284, realloc() does not respect...


You are not allowed to post comments on this tracker with your current authentication level.

bug #53284: realloc() does not respect __freelist size for small allocations

Submitter:  David Glaze <djglaze>
Submitted:  Sun 04 Mar 2018 10:19:44 PM UTC
Category:  Library Severity:  3 - Normal
Priority:  5 - Normal Item Group:  libc code
Status:  None Assigned to:  None
Percent Complete:  0% Open/Closed:  Open
Release:  2.0.0 Fixed Release:  None

Fri 17 Jan 2020 05:58:01 AM UTC, comment #2: 

Related other bugs: #40535, #32702

Petteri Aimonen <jpa_fi>
Tue 31 Dec 2019 10:41:55 AM UTC, comment #1: 

This got triggered by nanopb fuzz tests; I'm adding a workaround on my library side as this would otherwise have potential security implications. It would be great if this could eventually be fixed on avr-libc side.

I think the suggested patch does not address the case of len == 0, which should be equivalent to free(ptr). I've attached a patch that handles that also.

(file #48143)

Petteri Aimonen <jpa_fi>
Sun 04 Mar 2018 10:19:44 PM UTC, original submission:  

The realloc() function does not respect the __freelist size when resizing an allocation down to 0 or 1 bytes.  If this allocation is then deallocated with free(), a __freelist entry is placed in its slot that can partially overwrite an adjacent allocation, leading to irrecoverable memory corruption.  The following code can reproduce the corruption:

// Relevant bytes in heap allocation shown in comments (User storage denoted with 0xFF)
char * resizedVar = (char*)malloc(6);       // 0x06 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
resizedVar = (char*)realloc(resizedVar, 1); // 0x01 0x00 0xFF
char * fixedVar = (char*)malloc(4);         // 0x01 0x00 0xFF 0x04 0x00 0xFF 0xFF 0xFF 0xFF
free(resizedVar);                           // 0x01 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF
                                            //                  ^ Size of fixedVar overwritten

malloc() handles this issue with a minimum allocation size, so that requested allocations of 0, 1, or 2 bytes all take the same storage.  The same technique can be used here to prevent problems, as shown in the attached patch.

David Glaze <djglaze>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #43463:  realloc_small_size.patch added by djglaze (614B - text/x-patch)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by jpa_fi (Updated the item)
  • -email is unavailable- added by djglaze (Submitted the item)

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-12-31 jpa_fi Attached File- Added avr-libc-realloc-small-len-values.patch, #48143
    2018-03-04 djglaze Attached File- Added realloc_small_size.patch, #43463

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code