Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-of-bounds read in parse_char_class() #139

Closed
RKX1209 opened this issue Jul 31, 2019 · 1 comment · Fixed by #140
Closed

Out-of-bounds read in parse_char_class() #139

RKX1209 opened this issue Jul 31, 2019 · 1 comment · Fixed by #140

Comments

@RKX1209
Copy link

RKX1209 commented Jul 31, 2019

When Onigmo try to parse a regular expression "[{\x{111111}x", it causes out of bounds read in parse_char_class().

Here is a POC code based on sample/syntax.c

/*
 * perl_syntax2.c
 */
#include <stdio.h>
#include <string.h>
#include "onigmo.h"

extern int exec(const OnigSyntaxType* syntax,
		char* apattern, char* astr)
{
  int r;
  unsigned char *start, *range, *end;
  regex_t* reg;
  OnigErrorInfo einfo;
  OnigRegion *region;
  UChar* pattern = (UChar* )apattern;
  UChar* str     = (UChar* )astr;

  fprintf(stderr, "pattern %s (%lu)\n", pattern, strlen((char*)pattern));
  r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
	       ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, syntax, &einfo);

  if (r != ONIG_NORMAL) {
    OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str(s, r, &einfo);
    fprintf(stderr, "ERROR: %s\n", s);
    return -1;
  }

  region = onig_region_new();

  end   = str + strlen((char* )str);
  start = str;
  range = end;
  r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
  if (r >= 0) {
    int i;

    fprintf(stderr, "match at %d\n", r);
    for (i = 0; i < region->num_regs; i++) {
      fprintf(stderr, "%d: (%ld-%ld)\n", i, region->beg[i], region->end[i]);
    }
    r = 0;
  }
  else if (r == ONIG_MISMATCH) {
    fprintf(stderr, "search fail\n");
    r = -1;
  }
  else { /* error */
    OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str(s, r);
    fprintf(stderr, "ERROR: %s\n", s);
    return -1;
  }

  onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
  onig_free(reg);
  onig_end();
  return r;
}

extern int main(int argc, char* argv[])
{
  int r = 0;

  char regex[] = "[{\\x{111111}x";

  r |= exec(ONIG_SYNTAX_PERL,
  	    regex,
  	   "bgh8x");    
  onig_end();
  return r;
}
gcc -o perl_syntax2 perl_syntax2.c  -lonigmo
./perl_syntax2 
pattern [{\x{111111}x (13)
Segmentation fault (core dumped)

Here is a crash log.

$ gdb -q ./perl_syntax2 -c core
Core was generated by `./perl_syntax2'.                                                   
Program terminated with signal SIGSEGV, Segmentation fault.                               
#0  0x00007f324371343d in next_state_val (cc=cc@entry=0x56418051b460,                     
    asc_cc=asc_cc@entry=0x0, from=from@entry=0x7ffc2b22a988, to=120,                      
    from_israw=from_israw@entry=0x7ffc2b22a994, to_israw=to_israw@entry=0,                
    intype=CCV_SB, type=0x7ffc2b22a990, state=0x7ffc2b22a98c, env=0x7ffc2b22ad60)         
    at regparse.c:4461                                                                    
4461          BITSET_SET_BIT_CHKDUP(cc->bs, (int )(*from));                               
(gdb) bt                                                                                  
#0  0x00007f324371343d in next_state_val (cc=cc@entry=0x56418051b460,                     
    asc_cc=asc_cc@entry=0x0, from=from@entry=0x7ffc2b22a988, to=120,                      
    from_israw=from_israw@entry=0x7ffc2b22a994, to_israw=to_israw@entry=0,                
    intype=CCV_SB, type=0x7ffc2b22a990, state=0x7ffc2b22a98c, env=0x7ffc2b22ad60)         
    at regparse.c:4461                                                                    
#1  0x00007f32437174e9 in parse_char_class (np=0x7ffc2b22abc0,                            
    asc_np=asc_np@entry=0x7ffc2b22aac8, tok=tok@entry=0x7ffc2b22ac80,                     
    src=src@entry=0x7ffc2b22ac78, end=0x7ffc2b22b1e7 "", env=0x7ffc2b22ad60)              
    at regparse.c:4704                                                                    
#2  0x00007f324371a862 in parse_exp (np=np@entry=0x7ffc2b22abc0,                          
    tok=tok@entry=0x7ffc2b22ac80, term=term@entry=0, src=src@entry=0x7ffc2b22ac78,
    end=end@entry=0x7ffc2b22b1e7 "", env=env@entry=0x7ffc2b22ad60) at regparse.c:6334
#3  0x00007f324371cb74 in parse_branch (top=top@entry=0x7ffc2b22ac20, 
    tok=tok@entry=0x7ffc2b22ac80, term=term@entry=0, src=src@entry=0x7ffc2b22ac78, 
    end=end@entry=0x7ffc2b22b1e7 "", env=env@entry=0x7ffc2b22ad60) at regparse.c:6503
#4  0x00007f324371cd14 in parse_subexp (top=top@entry=0x7ffc2b22ad18, 
    tok=tok@entry=0x7ffc2b22ac80, term=term@entry=0, src=src@entry=0x7ffc2b22ac78, 
    end=end@entry=0x7ffc2b22b1e7 "", env=env@entry=0x7ffc2b22ad60) at regparse.c:6549
#5  0x00007f324371cf71 in parse_regexp (env=0x7ffc2b22ad60, end=0x7ffc2b22b1e7 "", 
    src=0x7ffc2b22ac78, top=0x7ffc2b22ad18) at regparse.c:6598
#6  onig_parse_make_tree (root=root@entry=0x7ffc2b22ad18, 
    pattern=pattern@entry=0x7ffc2b22b1da "[{\\x{111111}x", end=0x7ffc2b22b1e7 "", 
    reg=reg@entry=0x56418051b260, env=env@entry=0x7ffc2b22ad60) at regparse.c:6643
#7  0x00007f32437240f8 in onig_compile (reg=0x56418051b260, 
    pattern=pattern@entry=0x7ffc2b22b1da "[{\\x{111111}x", 
    pattern_end=pattern_end@entry=0x7ffc2b22b1e7 "", einfo=einfo@entry=0x7ffc2b22b120)
    at regcomp.c:5659
#8  0x00007f3243724a3b in onig_new (reg=0x7ffc2b22b0e8, 
    pattern=0x7ffc2b22b1da "[{\\x{111111}x", pattern_end=0x7ffc2b22b1e7 "", option=0, 
    enc=0x56417f01cca0 <OnigEncodingASCII>, syntax=0x56417f01cd40 <OnigSyntaxPerl>, 
    einfo=0x7ffc2b22b120) at regcomp.c:5887
#9  0x000056417ee1bbe1 in exec (syntax=0x56417f01cd40 <OnigSyntaxPerl>, 
    apattern=0x7ffc2b22b1da "[{\\x{111111}x", astr=0x56417ee1bf9a "bgh8x")
    at ./perl_syntax2.c:20
#10 0x000056417ee1bea9 in main (argc=1, argv=0x7ffc2b22b2d8) at ./perl_syntax2.c:68

Thanks

k-takata added a commit that referenced this issue Aug 1, 2019
/[\x{111111}]/ causes out-of-bounds read when encoding is a single byte
encoding. \x{111111} is an invalid codepoint for a single byte encoding.
Check if it is a valid codepoint.
@k-takata
Copy link
Owner

k-takata commented Aug 1, 2019

Thank you for the report.
I confirmed that this happens only when a single byte encoding is used.
I added checks for invalid codepoints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants