Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nesting guard to avoid "out of stack space" #2438

Merged
merged 1 commit into from
Jul 24, 2017

Conversation

mgreter
Copy link
Contributor

@mgreter mgreter commented Jul 11, 2017

Note that this limit is not an exact science
it depends on various factors, which some are
not under our control (compile time or even OS
dependent settings on the available stack size)
It should fix most common segfault cases though.

Fixing this correctly would need a major refactoring
of the parser to avoid any recursing function calls.
IMO the limit should be pretty safe and it at least
allows somebody to finetune libsass if they need.

Fixes 5+ issues as reported on hackerone

Note that this limit is not an exact science
it depends on various factors, which some are
not under our control (compile time or even OS
dependent settings on the available stack size)
It should fix most common segfault cases though.
@xzyfer xzyfer added this to the 3.5.0.beta.4 milestone Jul 12, 2017
@mgreter mgreter merged commit 73ea2e2 into sass:master Jul 24, 2017

#define NESTING_GUARD(name) \
LocalOption<size_t> cnt_##name(name, name + 1); \
if (nestings > MAX_NESTING) throw Exception::NestingLimitError(pstate); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should nestings be name here?

Copy link
Contributor Author

@mgreter mgreter Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the Macro is called as NESTING_GUARD(nestings), therefore it will in turn insert a call to LocalOption<size_t> cnt_nestings(nestings, nestings + 1). Meaning it will store the previous value and then increase it by one (reverting once local variable cnt_nesting goes out of scope).

Sorry, read your statement the reverse way. Yeah, the second line should be name I guess.

@xzyfer
Copy link
Contributor

xzyfer commented Oct 6, 2017

This commit is failing on the 3.4-stable branch, on mingw. Not exactly sure why but I've track it down to the default argument assignment of msg here

NestingLimitError(ParserState pstate, std::string msg = def_nesting_limit, std::vector<Sass_Import_Entry>* import_stack = 0);

See https://ci.appveyor.com/project/sass/libsass/build/1.0.3281

@mgreter
Copy link
Contributor Author

mgreter commented Oct 6, 2017

Cannot reproduce locally against https://github.com/sass/sass-spec/tree/b744c1036a47681c45b3a2b0eee915ae24166e5e (3.4 seems to be removed in latest specs). Also pretty unlikely to be the cause for the failing at-root-alone-itpl/basic-alone-itpl tests. We also use the same default argument assignment in https://github.com/sass/libsass/pull/2438/files#diff-3f483a2c484893970bcb41dc1c2dac79L29 for quite some time ...

@mgreter
Copy link
Contributor Author

mgreter commented Oct 6, 2017

Issue seems related to the length of the sassc invocation.

works: sassc\bin\sassc.exe sass-spec\spec\libsass\base-level-parent\root\basic-alone-itpl\input.scss
crashes: sassc\bin\sassc.exe sass-spec\..\sass-spec\spec\libsass\base-level-parent\root\basic-alone-itpl\input.scss
crashes: sassc\bin\sassc.exe sass-spec\spec\..\spec\libsass\base-level-parent\root\basic-alone-itpl\input.scss
works: ..\sassc\bin\sassc.exe spec\..\spec\libsass\base-level-parent\root\basic-alone-itpl\input.scss

@mgreter
Copy link
Contributor Author

mgreter commented Oct 7, 2017

Debugged it to the main memory corruption: #2484

@xzyfer
Copy link
Contributor

xzyfer commented Oct 9, 2017

Nice find

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants