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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/2656
## Description:
Issue: sass/libsass#2656
Title: AddressSanitizer: heap-buffer-overflow (OOB read) in Sass::Prelexer::exactly (libsass/src/lexer.hpp:93:14)
State: closed
Created by: glen-mac
Created at: 2018-06-02 00:13:09+00:00
Labels: Fuzzy

Issue Body:
Hey there, I have discovered an out-of-bands read (OOB) in libsass at: [libsass/src/lexer.hpp:93:14](https://github.com/sass/libsass/tree/60f839158eff1f1bb84cb44f0f0c4bce3ed86a6d/src/lexer.hpp#L93)

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 until the commit tested above.

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

The full ASAN report can be found [here](https://github.com/sass/libsass/files/2054417/asan.zip) (symbols names too long to bother showing).
```
==14124==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60d00000cff1 at pc 0x0000006be6c1 bp 0x7fff2a90 0fc0 sp 0x7fff2a900fb8
READ of size 1 at 0x60d00000cff1 thread T0
    #0 0x6be6c0 in char const* Sass::Prelexer::exactly<(char)92>(char const*) /home/glenn/temp/libsass/src/lexer.hpp:93:14
...
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/glenn/temp/libsass/src/lexer.hpp:93:14 in char const* Sass::Prelexer::exactly<(char)92>(char const*)
...
```


Comments:
Comment by glebm on 2018-11-25 12:09:15+00:00:
Only the following case still fails: `id:000449,sig:06,src:008670+009131,op:splice,rep:8`. A memory leak not an OOB though.

Attachments:
https://github.com/sass/libsass/files/2054426/crash_files.zip
https://github.com/sass/libsass/files/2054417/asan.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;
}