cqcallaw / newt

The newt programming language
GNU General Public License v3.0
12 stars 2 forks source link

heap-buffer-overflow in UnaryExpression::Evaluate #79

Closed gy741 closed 6 years ago

gy741 commented 6 years ago

Hello.

I found a heap-buffer-overflow in newt.

Please confirm.

Thanks.

Summary: heap-buffer-overflow OS: CentOS 7 64bit Version: d3bcd7f187e0f318ec606d936297b0e06e8aac47 PoC Download: OV_UnaryExpression__Evaluate.zip

karas@karas-sec:~/newt/Release$ xxd OV_UnaryExpression__Evaluate
00000000: 7072 696e 7428 2121 2d2d 2d2d 2d2d 3125  print(!!------1%
00000010: 2121 212d 2821 323e 2d2d 2d2d 2d2d 3125  !!!-(!2>------1%
00000020: 2121 2121 0935 2929                      !!!!.5))

Steps to reproduce: 1.Download the .POC files. 2.Compile the source code with ASan. 3.Execute the following command : ./newt $FILE

=================================================================
==21551==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000290 at pc 0x000001874c95 bp 0x7fff6a73e570 sp 0x7fff6a73e568
READ of size 4 at 0x602000000290 thread T0
    #0 0x1874c94 in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:122:20
    #1 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #2 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #3 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #4 0x14964dc in BinaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/binary_expression.cpp:105:49
    #5 0x160dee3 in Expression::ToString[abi:cxx11](std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/expression.cpp:38:40
    #6 0x13fbdeb in PrintStatement::Execute(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/print_statement.cpp:42:37
    #7 0x143040f in StatementBlock::Execute(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/statement_block.cpp:81:38
    #8 0x142dbb2 in StatementBlock::Execute(std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/statement_block.cpp:71:9
    #9 0x1b0bbdd in main /home/karas/newt/Release/../src/newt.cpp:187:32
    #10 0x7fd37fe041c0 in __libc_start_main /build/glibc-itYbWN/glibc-2.26/csu/../csu/libc-start.c:308
    #11 0x426749 in _start (/home/karas/newt/Release/newt+0x426749)

0x602000000291 is located 0 bytes to the right of 1-byte region [0x602000000290,0x602000000291)
allocated by thread T0 here:
    #0 0x51eba0 in operator new(unsigned long) (/home/karas/newt/Release/newt+0x51eba0)
    #1 0x15a4ad0 in ComparisonExpression::compute(int const&, int const&, yy::location, yy::location) const /home/karas/newt/Release/../src/expressions/comparison_expression.cpp:101:34
    #2 0x14a7ca7 in BinaryExpression::compute(bool const&, int const&, yy::location, yy::location) const /home/karas/newt/Release/../src/expressions/binary_expression.cpp:399:9
    #3 0x14a7ca7 in BinaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/binary_expression.cpp:148
    #4 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #5 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #6 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #7 0x1868b5c in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/unary_expression.cpp:107:55
    #8 0x14964dc in BinaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/binary_expression.cpp:105:49
    #9 0x160dee3 in Expression::ToString[abi:cxx11](std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/expressions/expression.cpp:38:40
    #10 0x13fbdeb in PrintStatement::Execute(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/print_statement.cpp:42:37
    #11 0x143040f in StatementBlock::Execute(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/statement_block.cpp:81:38
    #12 0x142dbb2 in StatementBlock::Execute(std::shared_ptr<ExecutionContext>) const /home/karas/newt/Release/../src/statements/statement_block.cpp:71:9
    #13 0x1b0bbdd in main /home/karas/newt/Release/../src/newt.cpp:187:32
    #14 0x7fd37fe041c0 in __libc_start_main /build/glibc-itYbWN/glibc-2.26/csu/../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/karas/newt/Release/../src/expressions/unary_expression.cpp:122:20 in UnaryExpression::Evaluate(std::shared_ptr<ExecutionContext>, std::shared_ptr<ExecutionContext>) const
Shadow bytes around the buggy address:
  0x0c047fff8000: fa fa fd fd fa fa fd fa fa fa fd fa fa fa fd fa
  0x0c047fff8010: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fa
  0x0c047fff8020: fa fa 00 00 fa fa 00 00 fa fa 04 fa fa fa 00 00
  0x0c047fff8030: fa fa 01 fa fa fa 00 00 fa fa 01 fa fa fa 04 fa
  0x0c047fff8040: fa fa 04 fa fa fa 04 fa fa fa 04 fa fa fa fd fa
=>0x0c047fff8050: fa fa[01]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==21551==ABORTING

========== [Acknowledgement] This work was supported by ICT R&D program of MSIP/IITP. [R7518-16-1001, Innovation hub for high Performance Computing]

cqcallaw commented 6 years ago

@gy741 Thanks for the report! This one is particularly interesting because xxd seems to decode the third-from-last character as a dot ('.') instead of a tab:

$ xxd bug.nwt 
00000000: 7072 696e 7428 2121 2d2d 2d2d 2d2d 3125  print(!!------1%
00000010: 2121 212d 2821 323e 2d2d 2d2d 2d2d 3125  !!!-(!2>------1%
00000020: 2121 2121 0935 2929                      !!!!.5))
$ cat bug.nwt 
print(!!------1%!!!-(!2>------1%!!!!    5))

I think the behavior of cat is correct in this case. Either way, the heap overflow needs to be resolved.