New issue
Advanced search Search tips
Starred by 3 users
Status: Fixed
Owner:
Closed: Aug 2015
Cc:



Sign in to add a comment
Bad alloca in OS X regex engine (TRE)
Project Member Reported by ianbeer@google.com, Jun 4 2015 Back to list
The OS X regex engine (TRE) uses the alloca function in a few places, sometimes where an attacker can partially control the size, eg:

static int
tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len,
	  tre_str_type_t type, size_t nmatch, regmatch_t pmatch[],
	  int eflags)
{
  reg_errcode_t status;
  tre_tag_t *tags = NULL;
  int eo;
  size_t offset = 0, count = 0;
  if (tnfa->num_tags > 0 && nmatch > 0)
    {
#ifdef TRE_USE_ALLOCA
      tags = alloca(sizeof(*tags) * tnfa->num_tags);  <-- this is called

num_tags is computed based on the complexity of the regex. It's quite easy to make num_tags large enough for the alloca call to try to allocate more than the available stack space.

OS X alloca is a simple stack pointer subtraction; there are no checks that it's safe or stack-growing by touching each page.

The main process stack has a >50M guard region below it which makes this difficult to hit on the main thread (as the input regex would have to be too long) but pthread stacks are much smaller (512k) and only have a single guard page meaning it's easy to force an alloca which is too large for them. See attached PoC for details of the regex to do this.
 
tre_alloca.c
1.4 KB Download
Project Member Comment 1 by ianbeer@google.com, Jun 10 2015
Labels: Reported-2015-Jun-10 Id-623973495
Project Member Comment 2 by ianbeer@google.com, Aug 14 2015
Labels: CVE-2015-3797 Fixed-2015-Aug-13
OS X advisory: https://support.apple.com/en-us/HT205031
iOS advisory: https://support.apple.com/en-us/HT205030
Project Member Comment 3 by ianbeer@google.com, Aug 14 2015
Labels: Product-iOS
Status: Fixed
Project Member Comment 4 by ianbeer@google.com, Sep 21 2015
Labels: -Restrict-View-Commit
Sign in to add a comment