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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sass/libsass/issues/2671
## Description:
Issue: sass/libsass#2671
Title: AddressSanitizer: stack-overflow at Sass::Inspect::operator() (inspect.cpp:977)
State: closed
Created by: hongxuchen
Created at: 2018-06-03 04:54:21+00:00

Issue Body:
We found with our fuzzer some stack over flow errors at Sass::Inspect::operator() (inspect.cpp:977)(45f50873962b7d1c66bd115ba6e644bdaaf6cac1) when compiled with Address Sanitizer (using sassc as the driver).

```
=================================================================
==2828==ERROR: AddressSanitizer: stack-overflow on address 0x7ffd23974fd8 (pc 0x7f7c014511a4 bp 0x7ffd23975850 sp 0x7ffd23974fe0 T0)
    #0 0x7f7c014511a3 in __interceptor_strlen (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x701a3)
    #1 0x7f7bffced43b in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(char const*) const (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x12143b)
    #2 0x7f7c010a3c86 in bool std::operator==<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) /usr/include/c++/5/bits/basic_string.h:4939
    #3 0x7f7c010a3c86 in Sass::Inspect::operator()(Sass::Wrapped_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:977
    #4 0x7f7c0109de50 in Sass::Inspect::operator()(Sass::Compound_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:996
    #5 0x7f7c010abed7 in Sass::Compound_Selector::perform(Sass::Operation<void>*) /home/hongxu/FUZZ/libsass-orig/src/ast.hpp:2742
    #6 0x7f7c010abed7 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:1023
    #7 0x7f7c010ac3f4 in Sass::Complex_Selector::perform(Sass::Operation<void>*) /home/hongxu/FUZZ/libsass-orig/src/ast.hpp:2907
    #8 0x7f7c010ac3f4 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:1061
...
    #447 0x7f7c010abed7 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:1023
    #448 0x7f7c010ac3f4 in Sass::Complex_Selector::perform(Sass::Operation<void>*) /home/hongxu/FUZZ/libsass-orig/src/ast.hpp:2907
    #449 0x7f7c010ac3f4 in Sass::Inspect::operator()(Sass::Complex_Selector*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:1061
    #450 0x7f7c010ae63b in Sass::Complex_Selector::perform(Sass::Operation<void>*) /home/hongxu/FUZZ/libsass-orig/src/ast.hpp:2907
    #451 0x7f7c010ae63b in Sass::Inspect::operator()(Sass::Selector_List*) /home/hongxu/FUZZ/libsass-orig/src/inspect.cpp:1098

SUMMARY: AddressSanitizer: stack-overflow ??:0 __interceptor_strlen
==2828==ABORTING
```

Sample input files:
[so_inspect.cpp:977_01.txt](https://github.com/sass/libsass/files/2065675/so_inspect.cpp.977_01.txt)
[so_inspect.cpp:977_02.txt](https://github.com/sass/libsass/files/2065676/so_inspect.cpp.977_02.txt)



Comments:
Comment by glebm on 2019-04-10 19:24:08+00:00:
This is fixed on master (not sure by which commit)

---

Comment by xi on 2019-06-05 05:36:06+00:00:
Assigned CVE-2018-20822

---

Comment by NicoleG25 on 2020-05-27 13:58:02+00:00:
Hi @glebm , could you perhaps point me in the direction of the commit fixing this issue?
I was thinking it could be https://github.com/sass/libsass/commit/dfe23ac550de902289e5e5d5f4ece794afbb68d3
But I'm not quite sure. 
Thanks in advance !

---

Comment by glebm on 2020-05-27 17:07:10+00:00:
@NicoleG25 I don't know which commit had fixed it, you could bisect to find out

Attachments:
https://github.com/sass/libsass/files/2065676/so_inspect.cpp.977_02.txt
https://github.com/sass/libsass/files/2065675/so_inspect.cpp.977_01.txt

Commit References:
dfe23ac550de902289e5e5d5f4ece794afbb68d3
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 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;
}