+1  A: 

PCRE is calling two functions recursively repeatedly, until it runs out of stack space. You can either try to upgrade the Rewrite DLL to one that isn't buggy, or try to change your rewrite rules so that it doesn't hit the PCRE bug.

Paul Betts
This is not a bug in the rewriter. This is a bug in the pattern and configuration. the stack overflow is happening in pcre_exec, which is where the regular expression is being executed. It's a degenerate case but one that should be handled in config (eg, drop all URLs with % in them).
Cheeso
Any Regex parser that can be convinced to stack overflow on a certain input has a bug; though I agree that it might be easiest to fix this in the configuration
Paul Betts
Any general-purpose regex allows repeating null groups, which immediately allows the possibility of infinite loops. Conclusion: *all* regex engines are buggy!
Cheeso
So you're saying all those web Regex sites can easily be brought down by typing in a repeating null group into it
Paul Betts
NO. I'm saying, it's possible to build a regex that recurses on itself, regardless of the input. And, I'm also saying (or I should have said), it is easier to build a broken regex with more complicated inputs. ANY regex parser that is general enough supports recursion, and unless it has a failsafe check in the recursion logic, it will overflow. PCRE has a failsafe in it (sas the doc), but it apparently is not working in this case. Anyway I worked with Mase to change his regex and avoid the problem.
Cheeso
+1  A: 

looks like you've got pcre_exec recursing and using up all of your stack space. I would check and see what regular expressions you are using.

jcopenha
A: 

Based on that stack trace at the very bottom, it looks like the program is going into infinite recursion, with two functions that are calling each other without terminating. This will cause a stack overflow exception due to eventually running out of available stack.

Tyler McHenry
A: 

Can you figure out the URL that's sending into the infinite recursion? It looks like you have two rewrite rules that are triggering each other. Unless you have the source to IsapiRewrite4.dll, it's not going to help much -- you can get to the assembly code -- but even if you had the source (you could decompile), it won't help, because I think you need to see the URL that got passed.

Did it also dump memory (or could you make it do that). Arg1, Arg2, or Arg3 might be a pointer to the URL?

Lou Franco
I DO have the source for IsapieRewrite4.dll. It's open source, available on the [IIRF website](http://www.codeplex.com/IIRF)
MaseBase
1. make a build with a pdb 2. deploy the dll with the pdb in the same dir 3. repro problem 4. (maybe debug diag will read pdb), if not, load IIS with the dll on your local machine and the pdb in the same directory. Find the function in the error. 5. add logging to the function so you know what URL is being rewritten -- see if you can figure out how many times the function is on the stack (thread local variable counter?), and if it's there more than a few times, log what is happening. 5. Run the new dll in prodcution.
Lou Franco
Thanks Lou... if I could repro the problem, I wouldn't be posting this question. Also, I tried deploying the DLL with the PDB, but the debug diag did not read the symbols, it's saying it can't find them.
MaseBase
ok, but now that you have a PDB, load IIS on your Dev box with the ISAPI DLL and go to the address in the debug diag -- you should be able to see symbols (put the PDB in the same directory as the DLL). I don't think this will help much as I think the problem is in the URL that was actually passed. Maybe you can patch by having the function detect that it's in a mutually recursive loop and exiting with an error or exception.
Lou Franco
You don't need a debuggable version of IIRF. It's clear the stack overflow is happening in the pcre_exec function, which is where the Regular Expression is evaluated/executed. It is overflowing because the rule is looping. This is not uncommon in regex processing. Be careful!
Cheeso
A: 

So I believe I have found the cause error. Though I'm not sure if this is still really a bug in IIRF ISAPI filter? It at least should not iterate through the rewrite functions so many times that it causes a stack overflow.

I can reproduce the error I was seeing in the event log by requesting this URL on my server: [original_url]/Forum+Result:+%E8%F1%EF%EE%EB%FC%E7%EE%E2%E0%ED+%ED%E8 %EA%ED%E5%E9%EC+%22Rifsadasy%22;%EF%E8%EA%F2%EE%EA%EE%E4+%E4%E5%F8%E8 %F4%F0%EE%E2%E0%ED;%E7%E0%F0%E5%E3%E8%F1%F2%F0%E8%F0%EE%E2%E0%EB%E8%F1 %FC+100%25+%28%E2%EA%EB%FE%F7%E5%ED+%F0%E5%E6%E8%EC+%F2%EE%EB%FC%EA%EE +%F0%E5%E3%E8%F1%F2%F0%E0%F6%E8%E8%29;

So I've created a rewrite rule to return a 404 status for this URL.

But I needed to know if this was some type of attack, I cannot be fully sure yet, but here is what I think the encrypted string says. The URL was coming from IP addresses in Russia, so here is what I've done to translate it:

  1. Hex -to- ASCII:

    èñïîëüçîâàí+íèêíåéì+"Rifsadasy";ïèêòîêîä+äåøèôðîâàí;çàðåãèñòðèðîâàëèñü+100%+(âêëþ÷åí+ðåæèì+òîëüêî+ðåãèñòðàöèè)

  2. Then Cyrillic to Russian:

    использован+никнейм+"Rifsadasy";пиктокод+дешифрован;зарегистрировались+100%+(включен+режим+только+регистрации)

  3. Then Russian to English:

    used nickname "Rifsadasy"; piktokod decrypt; registered 100 (mode only)
    or, similarly
    It is used никнейм "Rifsadasy"; пиктокод it is decoded; were registered 100 (the mode only registration is included)

MaseBase
You've done the right thing. Reject those long URLs, protect your server.
Cheeso
+1  A: 

I think the stack overflow is not due to a bug in the rewriter. It is due to a bug in the patterns used in the configuration of the filter configuration.

It is actually easy to create a single regex that loops endlessly, and neither PCRE nor IIRF have a way to prevent that. It is also possible to create logic loops in the rewrite rules, so that you redirect or rewrite endlessly. Again, there's no way for the filter to stop you from shooting yourself in the foot. You have to take care. These risks exist when using any rewriter that depends on PCRE, or any modular regex engine.

The stack overflow is happening in pcre_exec, which is where the regular expression is being executed. It's a degenerate case but one that should be handled in the configuration. A prior rule should have filtered out such invalid cases. Here's a post about using a rewriting filter as a security barrier.

Test early and often. Some people think that because the filter rules are "just configuration", it is not in need of rigorous testing. In general, that's not a safe assumption. Treat the IIRF rules like any code module. You're just using a different language.

Cheeso
Cheeso! SO GLAD to see you chime in here... I have been needing your help. My understanding was the IterationLimit setting prevented this from becoming a problem. Setting the IterationLimit to 5 or 10 meant that one URL would stop being rewritten after 10 rewrites. In re-reading the desc of this setting, it seems that the iteration limit only applies to one pass through the rules. Once a URL enters the top of the ruleset again, the iteration limit is reset. Is this correct?Either way, now I know to begin dissecting my rules to find out where the loop is getting caught.
MaseBase