views:

69

answers:

3

Apologies for the shortness of the question, however I don't think it needs much elaboration.

Any there any security implications caused by using the CSharpCodeProvider and could it open a server up for attack?

+1  A: 

Sort of. On the surface it's not a direct risk, because you're not running code, just compiling it. However, there's nothing that says that the C# compiler doesn't contain some sort of bug that, given the right malicious input, would cause it to bail out and start executing commands directly.

However, if you later execute the compiled code (and presumably you do -- otherwise why would you compile it to begin with?), it will be running the same context as you are. Obviously, that has all kinds of unpleasant security implications, much like using the quasi-analogous eval() feature of other languages.

John Feminella
@John - I wont ever be running an executable. The input is just used to ensure some code compiles. So, aside from the compiler having a bug, does this mean it's perfectly safe for the example I've just mentioned?
GenericTypeTea
If you're just checking that some code is syntactically valid and compiles without errors, I can't see the harm.
John Feminella
A: 

It depends on the source that you are compiling. If you have enough control over the source, then it might be an acceptable risk. If you are allowing someone outside of your sphere of trust supply code to the compiler, it might be an unacceptable risk.

Robaticus
+3  A: 

It depends on how you use it. Here is a summary sorted from the safe use to a use that you certainly don't want to allow (when running the code on a server or some environment that you want to control):

  • If you use CSharpCodeProvider just for generating C# source code, then you only need a permission to save the generated files to some directory or to noting at all (if it is possible to get the code generated into a memory stream)

  • If you use it for compiling generated C# source, then you need a permission to run csc.exe (which may not be available in some limited environments such as shared hostings).

  • If you just generate files & compile them, then it probably won't be harmful (although someone could probably abuse your application to generate many, many files and attack the server using some kind of DOS attack.

  • If you also load & execute the generated code, then it depends on how you generate it. If you assume that there are no bugs in C#/CodeDOM and can guarantee that the generated code is safe, then you should be fine.

  • If your code contain things such as CodeSnippetExpression that can be provided by the user (in some way) than the user can write and run anything he or she wants on your server, so this would be potentially quite dangerous.

Tomas Petricek
Pretend for a moment this will be an open system available to anyone, how would they use a CodeSnippetExpression to cause havoc? I don't quite understand how it's used. The code will only compile the provided code with the settings: GenerateExecutable=false; GenerateInMemory=true; and will then be called with provider.CompileAssemblyFromSource.
GenericTypeTea
@GenericTypeTea: The last point was assuming that the previous thing is true - that is, you load and run the assembly.
Tomas Petricek
@Tomas -thanks +1
GenericTypeTea