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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/2665
## Description:
Issue: sass/libsass#2665
Title: AddressSanitizer: null pointer dereference (SEGV) in Sass::Inspect::operator (libsass/src/inspect.cpp:1060:9)
State: closed
Created by: glen-mac
Created at: 2018-06-03 02:39:40+00:00
Labels: Dev - PR Ready, Fuzzy

Issue Body:
Hey there, I have discovered a null pointer dereference in libsass at: [inspect.cpp:1060:9](https://github.com/sass/libsass/blob/60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d/src/inspect.cpp#L1060)

Found when fuzzing commit 60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d of libsass, using commit [aa6d5c6](https://github.com/sass/sassc/commit/aa6d5c635ea8faf44d542a23aaf85d27e5777d48) of sassc as a harness.

Compile flags to reproduce: 
```
CC=clang CXX=clang++ CFLAGS='-fsanitize=address -g -O2 -fno-omit-frame-pointer' CXXFLAGS=$CFLAGS make -C sassc -j8
```

System information:
```
$ uname -a
Linux s127422 3.13.0-137-generic #186-Ubuntu SMP Mon Dec 4 19:09:19 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
```

This bug was found to be in libsass releases from [3.3.2](https://github.com/sass/libsass/tree/3.3.2) until the commit fuzzed (60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d).

You can find a collection of PoC files that trigger the bug [here](https://github.com/sass/libsass/files/2065584/inspect_crashes.zip)

The full ASAN report is shown below:
```
↳ ./sassc.bin < crash.file
ASAN:DEADLYSIGNAL
=================================================================
==30272==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000818883 bp 0x7fffec548770 sp 0x7fffec548520 T0)
    #0 0x818882 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/glenn/temp/libsass/src/inspect.cpp:1060:9
    #1 0x818c95 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/glenn/temp/libsass/src/inspect.cpp:1076:15
    #2 0x819902 in Sass::Inspect::operator()(Sass::Selector_List*) /home/glenn/temp/libsass/src/inspect.cpp:1113:7
    #3 0x7fa9df in Sass::Output::operator()(Sass::Ruleset*) /home/glenn/temp/libsass/src/output.cpp:138:12
    #4 0x80029f in Sass::Inspect::operator()(Sass::Block*) /home/glenn/temp/libsass/src/inspect.cpp:32:7
    #5 0x54cc72 in Sass::Context::render(Sass::SharedImpl<Sass::Block>) /home/glenn/temp/libsass/src/context.cpp:506:5
    #6 0x5209d8 in sass_compiler_execute /home/glenn/temp/libsass/src/sass_context.cpp:506:44
    #7 0x51f6e1 in sass_compile_context(Sass_Context*, Sass::Context*) /home/glenn/temp/libsass/src/sass_context.cpp:376:7
    #8 0x51707d in compile_stdin /home/glenn/temp/findsass/sassc/sassc.c:138:5
    #9 0x517a8e in main /home/glenn/temp/findsass/sassc/sassc.c:375:18
    #10 0x7fa62309282f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291
    #11 0x4456d8 in _start (/home/glenn/temp/findsass/sassc.bin+0x4456d8)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/glenn/temp/libsass/src/inspect.cpp:1060:9 in Sass::Inspect::operator()(Sass::Complex_Selector*)
==30272==ABORTING
```


Comments:
Comment by xzyfer on 2018-07-04 10:27:50+00:00:
Unable to reproduce this with the PoC files.

Tested against
- libsass@60f83915
- sassc@aa6d5c6
- with `-fsanitize=address -fno-omit-frame-pointer`

---

Comment by xzyfer on 2018-07-04 10:37:45+00:00:
Although I cannot reproduce I can see conceptually that this error is possible.
Patched in #2687.

Thanks!

---

Comment by xi on 2019-06-05 05:33:55+00:00:
Assigned CVE-2018-11696

Attachments:
https://github.com/sass/libsass/files/2065584/inspect_crashes.zip

Commit References:
60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d
aa6d5c635ea8faf44d542a23aaf85d27e5777d48

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 1e52b74306b7d73a617396c912ca436dc55fd4d8
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;
}