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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/3001
## Description:
Issue: sass/libsass#3001
Title: SEGV in sassc
State: closed
Created by: c0d3xpl0it
Created at: 2019-10-07 14:51:08+00:00
Labels: Dev - Needs Test, Bug - Confirmed

Issue Body:
We found SEGV in sassc binary and sassc is complied with clang enabling ASAN.

**Machine Setup**
```
Machine : Ubuntu 16.04.3 LTS
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
Commit : 4da7c4b
Command : sassc POC
```
**Complilation** : CC=afl-clang-fast CXX=afl-clang-fast++ AFL_USE_ASAN=1 make -C sassc -j4
**POC** : [POC.scss.zip](https://github.com/sass/libsass/files/3697977/POC.scss.zip)

```scss
&&
```

**ASAN Output**
```
fuzzer@fuzzer:~/victim/libsass/sassc/bin$ ./sassc -v
sassc: 3.6.1-5-g507f0
libsass: 3.6.2
sass2scss: 1.1.1
sass: 3.5
fuzzer@fuzzer:~/victim/libsass/sassc/bin$

fuzzer@fuzzer:~/victim/libsass/sassc/bin$ ./sassc POC
ASAN:DEADLYSIGNAL
=================================================================
==24568==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000009634ba bp 0x7ffeca0c6ff0 sp 0x7ffeca0c68c0 T0)
    #0 0x9634b9 in Sass::SharedPtr::SharedPtr(Sass::SharedPtr const&) /home/fuzzer/victim/libsass/src/memory/SharedPtr.hpp:116:53
    #1 0x9634b9 in Sass::SharedImpl<Sass::SimpleSelector>::SharedImpl(Sass::SharedImpl<Sass::SimpleSelector> const&) /home/fuzzer/victim/libsass/src/memory/SharedPtr.hpp:183
    #2 0x9634b9 in Sass::Parser::parseCompoundSelector() /home/fuzzer/victim/libsass/src/parser_selectors.cpp:143
    #3 0x960761 in Sass::Parser::parseComplexSelector(bool) /home/fuzzer/victim/libsass/src/parser_selectors.cpp:42:47
    #4 0x9696f1 in Sass::Parser::parseSelectorList(bool) /home/fuzzer/victim/libsass/src/parser_selectors.cpp:82:36
    #5 0x833ef6 in Sass::Parser::parse_ruleset(Lookahead) /home/fuzzer/victim/libsass/src/parser.cpp:530:25
    #6 0x7ff8eb in Sass::Parser::parse_block_node(bool) /home/fuzzer/victim/libsass/src/parser.cpp:278:21
    #7 0x7f419f in Sass::Parser::parse_block_nodes(bool) /home/fuzzer/victim/libsass/src/parser.cpp:189:11
    #8 0x7eea0f in Sass::Parser::parse() /home/fuzzer/victim/libsass/src/parser.cpp:115:5
    #9 0x5981a5 in Sass::Context::register_resource(Sass::Include const&, Sass::Resource const&) /home/fuzzer/victim/libsass/src/context.cpp:306:22
    #10 0x5b17c3 in Sass::File_Context::parse() /home/fuzzer/victim/libsass/src/context.cpp:576:5
    #11 0x55195e in Sass::sass_parse_block(Sass_Compiler*) /home/fuzzer/victim/libsass/src/sass_context.cpp:180:22
    #12 0x55195e in sass_compiler_parse /home/fuzzer/victim/libsass/src/sass_context.cpp:434
    #13 0x5503d4 in sass_compile_context(Sass_Context*, Sass::Context*) /home/fuzzer/victim/libsass/src/sass_context.cpp:317:7
    #14 0x550ac1 in sass_compile_file_context /home/fuzzer/victim/libsass/src/sass_context.cpp:421:12
    #15 0x53f1ce in compile_file /home/fuzzer/victim/libsass/sassc/sassc.c:173:5
    #16 0x540284 in main /home/fuzzer/victim/libsass/sassc/sassc.c:387:18
    #17 0x7f578e25e82f in __libc_start_main /build/glibc-LK5gWL/glibc-2.23/csu/../csu/libc-start.c:291
    #18 0x46d1d8 in _start (/home/fuzzer/victim/libsass/sassc/bin/sassc+0x46d1d8)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/fuzzer/victim/libsass/src/memory/SharedPtr.hpp:116:53 in Sass::SharedPtr::SharedPtr(Sass::SharedPtr const&)
==24568==ABORTING
fuzzer@fuzzer:~/victim/libsass/sassc/bin$
```


Comments:
Comment by nluedtke on 2019-11-12 12:36:05+00:00:
Being tracked as [CVE-2019-18799](https://nvd.nist.gov/vuln/detail/CVE-2019-18799).

Attachments:
https://github.com/sass/libsass/files/3697977/POC.scss.zip

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 081bbbffe01b589f507a4aa0129e067944815914
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;
}