Project: libsass (sass/libsass), language: c++
Vulnerability instance: libsass.cve-2018-19837
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/2659
## Description:
Issue: sass/libsass#2659
Title: AddressSanitizer: stack-overflow in Eval::operator() (eval.cpp:563)
State: closed
Created by: hongxuchen
Created at: 2018-06-02 07:24:05+00:00
Labels: Bug - Confirmed, Dev - PR Ready, Back port to 3.5 stable, Fuzzy

Issue Body:
We found with our fuzzer some stack over flow errors in `Sass::Eval::operator()` (eval.cpp, 45f50873962b7d1c66bd115ba6e644bdaaf6cac1) when compiled with Address Sanitizer (using sassc as the driver).

```
ASAN:SIGSEGV
=================================================================
==17362==ERROR: AddressSanitizer: stack-overflow on address 0x7ffe362605b0 (pc 0x7f7c51f2f7fe bp 0x7ffe36261810 sp 0x7ffe36260510 T0)
    #0 0x7f7c51f2f7fd in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:563
    #1 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #2 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #3 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #4 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #5 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #6 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #7 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #8 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
...
    #248 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #249 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #250 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595
    #251 0x7f7c51f2fe37 in Sass::Eval::operator()(Sass::Binary_Expression*) /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:595

SUMMARY: AddressSanitizer: stack-overflow /home/hongxu/FUZZ/libsass-orig/src/eval.cpp:563 Sass::Eval::operator()(Sass::Binary_Expression*)
==17362==ABORTING
```

Sample input files:
[test_s102.txt](https://github.com/sass/libsass/files/2064773/test_s102.txt)
[test_s401.txt](https://github.com/sass/libsass/files/2064774/test_s401.txt)
[test_s601.txt](https://github.com/sass/libsass/files/2064775/test_s601.txt)


Comments:
Comment by xzyfer on 2018-06-21 10:13:35+00:00:
Thanks @HongxuChen. 
I've confirmed the issue with test_s102.txt. 
A fix has been provided in #2675.

---

Comment by xi on 2019-06-05 05:34:58+00:00:
Assigned CVE-2018-19837

Attachments:
https://github.com/sass/libsass/files/2064775/test_s601.txt
https://github.com/sass/libsass/files/2064773/test_s102.txt
https://github.com/sass/libsass/files/2064774/test_s401.txt

Commit References:
45f50873962b7d1c66bd115ba6e644bdaaf6cac1

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
export BUILD='static'
make -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool
RUN git clone https://github.com/sass/libsass libsass
RUN git -C libsass checkout 19fd2bf2e31413b5935e7feb87fec630b486feba
WORKDIR $SRC/libsass
COPY build.sh $SRC/

Additional files:
filename: data_context_fuzzer.cc
content: #include "sass.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  char* sass_data = (char*) malloc(sizeof(char) * size + 1);
  if (sass_data == NULL) return 0;

  memcpy(sass_data, data, size);
  sass_data[size] = '\0';

  struct Sass_Data_Context* ctx = sass_make_data_context(sass_data);
  if (ctx == NULL) {
    free(sass_data);
    return 0;
  }

  struct Sass_Options* options = sass_make_options();
  if (options == NULL) {
    sass_delete_data_context(ctx);
    return 0;
  }

  sass_option_set_output_style(options, SASS_STYLE_NESTED);
  sass_option_set_precision(options, 5);

  sass_data_context_set_options(ctx, options);
  sass_compile_data_context(ctx);

  sass_delete_data_context(ctx);
  sass_delete_options(options);

  return 0;
}