Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Check regex timeout in loops and repetitions #38091

Merged
merged 2 commits into from
May 31, 2019

Conversation

ViktorHofer
Copy link
Member

This is a cherry pick of the commit from .NET Core 2.2, resolving the issue disclosed in CVE-2019-0820.

Check the regex timeout in SetLoop and SetRepetition opcodes to avoid
the timeout not being handled.

@wtgodbe this needs to be in before branching of, the change was reviewed and approved in the servicing branches already, this is a simple cherry-pick. Therefore I'm merging this and if you notice any issues I'm happy to revert the change.

cc @danmosemsft

@ViktorHofer
Copy link
Member Author

CI isn't triggering...

Check the regex timeout in SetLoop and SetRepetition opcodes to avoid
the timeout not being handled.
// Emit code to check the timeout every 2000th-iteration.
Ldloc(_loopV);
Ldc(LoopTimeoutCheckCount);
Rem();
Copy link
Member

Choose a reason for hiding this comment

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

See comment on the non-emit version.

Copy link
Member

Choose a reason for hiding this comment

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

This comment wasn't address. Fine for this PR, but consider addressing it subsequently.

var regex = new Regex(@"a\s+", options, TimeSpan.FromSeconds(1));
string input = @"a" + new string(' ', 800_000_000) + @"b";

Assert.Throws<RegexMatchTimeoutException>(() => regex.Match(input));
Copy link
Member

Choose a reason for hiding this comment

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

Should we validate that the exception is thrown within some window of time?

Copy link
Member

Choose a reason for hiding this comment

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

It would have to have significant leeway (eg., within 2x or 3x) to account for vagaries of CI machines.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see it's a second. Maybe within 30 sec?

Copy link
Member

Choose a reason for hiding this comment

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

Assuming 800M will take much longer.

Copy link
Member

Choose a reason for hiding this comment

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

30 seconds, 1 minute, whatever we think is reasonable. My goal would just be that the test doesn't pass after the regex runs for an hour and then upon completion sees there was a timeout requested and throws :)

@wtgodbe
Copy link
Member

wtgodbe commented May 31, 2019

The added test cases are OOM-ing on Windows:

System.Text.RegularExpressions.Tests.RegexMatchTests/Match_Timeout_Loop_Throws(options: None)

System.OutOfMemoryException : Exception of type 'System.OutOfMemoryException' was thrown.

at System.String.Ctor(Char c, Int32 count) in //src/System.Private.CoreLib/shared/System/String.cs:line 294
at System.Text.RegularExpressions.Tests.RegexMatchTests.Match_Timeout_Loop_Throws(RegexOptions options) in /
/src/System.Text.RegularExpressions/tests/Regex.Match.Tests.cs:line 389

@ViktorHofer will you be able to resolve this by noon PST (~2 hours)? It may be alright if we need to push this out until preview7. Alternatively, given that the Core stack is branching 3 days before the rest of the stack, it may be that we can sneak this in over the weekend if it's critical enough (and if @mmitche, @danmosemsft, @leecow give it the green light)

@ViktorHofer
Copy link
Member Author

@wtgodbe feel free to merge this as soon as CI is "green".

@stpehentoub @danmosemsft I addressed most feedback except the timeout which I will do in a follow-up PR to not block the branch-off.

@danmoseley
Copy link
Member

I do not think it is critical to get into Preview 6. It is not go live.

@stephentoub stephentoub added the auto-merge Automatically merge PR once CI passes. label May 31, 2019
@ghost
Copy link

ghost commented May 31, 2019

Hello @stephentoub!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@wtgodbe
Copy link
Member

wtgodbe commented May 31, 2019

CI is actually done here, but the reporting isn't showing it. Merging.

@wtgodbe wtgodbe merged commit 40f031a into dotnet:master May 31, 2019
@danmoseley danmoseley deleted the CVE-2019-0820 branch December 8, 2020 17:41
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Check regex timeout in loops and repetitions

Check the regex timeout in SetLoop and SetRepetition opcodes to avoid
the timeout not being handled.

* PR feedback and fix OOM


Commit migrated from dotnet/corefx@40f031a
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.RegularExpressions auto-merge Automatically merge PR once CI passes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants