Bug 20155 - malloc: Inconsistent chunk size checks
Summary: malloc: Inconsistent chunk size checks
Status: NEW
Alias: None
Product: glibc
Classification: Unclassified
Component: malloc (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: ---
Assignee: dj@redhat.com
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-27 13:15 UTC by Florian Weimer
Modified: 2016-10-26 18:05 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Weimer 2016-05-27 13:15:16 UTC
sysmalloc sets a bunch with size 2 * SIZE_SZ:

          /* The fencepost takes at least MINSIZE bytes, because it might
             become the top chunk again later.  Note that a footer is set
             up, too, although the chunk is marked in use. */
          old_size = (old_size - MINSIZE) & ~MALLOC_ALIGN_MASK;
          set_head (chunk_at_offset (old_top, old_size + 2 * SIZE_SZ), 0 | PREV_INUSE);
          if (old_size >= MINSIZE)
            {
              set_head (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ) | PREV_INUSE);
              set_foot (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ));
              set_head (old_top, old_size | PREV_INUSE | NON_MAIN_ARENA);
              _int_free (av, old_top, 1);

(Setting the NON_MAIN_ARENA flag here is rather dubious.)

_int_free checks against 2 * SIZE_SZ:

    if (__builtin_expect (nextchunk->size <= 2 * SIZE_SZ, 0)
        || __builtin_expect (nextsize >= av->system_mem, 0))
      {
        errstr = "free(): invalid next size (normal)";
        goto errout;
      }

This only works because nextchunk->size is actually 2 * SIZE_SZ + 1: the PREV_INUSE flag is set.

We have a couple of other such comparisons which could also give unexpected results.  Whether this intended or not is unclear.

It's questionable to patch into the heap chunks smaller than MINSIZE (in the way sysmalloc does) because it violates heap invariants.

I don't know what to do about this.  Such subtle tricks certainly make changes to the code more difficult.