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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/2664
## Description:
Issue: sass/libsass#2664
Title: AddressSanitizer: null pointer dereference (SEGV) in Sass::Expand::operator (libsass/src/expand.cpp:249:28)
State: closed
Created by: glen-mac
Created at: 2018-06-03 02:31:46+00:00
Labels: Invalid - Duplicate, Fuzzy

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

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.5.0](https://github.com/sass/libsass/tree/3.5.0) through to [3.5.2](https://github.com/sass/libsass/tree/3.5.2) and then re-introduced in the commit 60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d.

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

The full ASAN report is shown below:
```
↳ ./sassc.bin < crash.file
ASAN:DEADLYSIGNAL
=================================================================
==6006==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000071bb37 bp 0x7ffee8b515d0 sp 0x7ffee8b51140 T0)
    #0 0x71bb36 in Sass::Expand::operator()(Sass::Declaration*) /home/glenn/temp/libsass/src/expand.cpp:258:28
    #1 0x731326 in Sass::Expand::append_block(Sass::Block*) /home/glenn/temp/libsass/src/expand.cpp:808:27
    #2 0x714f0a in Sass::Expand::operator()(Sass::Block*) /home/glenn/temp/libsass/src/expand.cpp:81:5
    #3 0x717116 in Sass::Expand::operator()(Sass::Ruleset*) /home/glenn/temp/libsass/src/expand.cpp:153:27
    #4 0x731326 in Sass::Expand::append_block(Sass::Block*) /home/glenn/temp/libsass/src/expand.cpp:808:27
    #5 0x714f0a in Sass::Expand::operator()(Sass::Block*) /home/glenn/temp/libsass/src/expand.cpp:81:5
    #6 0x5521f7 in Sass::Context::compile() /home/glenn/temp/libsass/src/context.cpp:659:12
    #7 0x55144c in Sass::Data_Context::parse() /home/glenn/temp/libsass/src/context.cpp:630:12
    #8 0x5200ca in Sass::sass_parse_block(Sass_Compiler*) /home/glenn/temp/libsass/src/sass_context.cpp:238:22
    #9 0x5200ca in sass_compiler_parse /home/glenn/temp/libsass/src/sass_context.cpp:487
    #10 0x51f6d9 in sass_compile_context(Sass_Context*, Sass::Context*) /home/glenn/temp/libsass/src/sass_context.cpp:375:7
    #11 0x51707d in compile_stdin /home/glenn/temp/findsass/sassc/sassc.c:138:5
    #12 0x517a8e in main /home/glenn/temp/findsass/sassc/sassc.c:375:18
    #13 0x7f6ce042a82f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291
    #14 0x4456d8 in _start (/home/glenn/temp/findsass/sassc.bin+0x4456d8)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/glenn/temp/libsass/src/expand.cpp:258:28 in Sass::Expand::operator()(Sass::Declaration*)
==6006==ABORTING
```


Comments:
Comment by xzyfer on 2018-07-04 11:02:58+00:00:
Fixed in 0bc35e3d26922229d5a3e3308860cf0fcee5d1cf.

Thanks.

Attachments:
https://github.com/sass/libsass/files/2065577/expand_crashes.zip

Commit References:
0bc35e3d26922229d5a3e3308860cf0fcee5d1cf
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 9cfe0df975945325e05d6dd39ee98224898d9a79
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;
}