2006-09-15 LLuis Sanchez Gual <lluis@novell.com>
authorLluis Sanchez <lluis@novell.com>
Fri, 15 Sep 2006 09:32:42 +0000 (09:32 -0000)
committerLluis Sanchez <lluis@novell.com>
Fri, 15 Sep 2006 09:32:42 +0000 (09:32 -0000)
* TempFileCollection.cs: Create files in a temporary subdirectory,
for security reasons.
* CodeCompiler.cs: Let TempFileCollection choose the temp dir.

svn path=/trunk/mcs/; revision=65441

mcs/class/System/System.CodeDom.Compiler/ChangeLog
mcs/class/System/System.CodeDom.Compiler/CodeCompiler.cs
mcs/class/System/System.CodeDom.Compiler/TempFileCollection.cs

index 8e01380d10d7086997b49cb4965ea820a3a37d46..6dfa163dd03c2d2e83f965479a9b975421bd19cb 100644 (file)
@@ -1,3 +1,9 @@
+2006-09-15  LLuis Sanchez Gual  <lluis@novell.com>
+
+       * TempFileCollection.cs: Create files in a temporary subdirectory,
+       for security reasons.
+       * CodeCompiler.cs: Let TempFileCollection choose the temp dir.
+
 2006-05-04  LLuis Sanchez Gual  <lluis@novell.com>
 
        * TempFileCollection.cs: Make sure generated file names
index c612ae286ba6550a0e1b5b6c2592a837ca35a670..f80945daee6f7f872d99d36b745d808296209aa2 100644 (file)
@@ -128,7 +128,7 @@ namespace System.CodeDom.Compiler {
                        if (null == fileNames)
                                throw new ArgumentNullException ("fileNames");
 
-                       options.TempFiles = new TempFileCollection (Path.GetTempPath ());
+                       options.TempFiles = new TempFileCollection ();
                        foreach (string file in fileNames) {
                                options.TempFiles.AddFile (file, keepFiles);
                        }
index 947e6faa656d9c22e1585bb8d2aa132a864d0a89..785ffb4aaa6bdeffe839759b8bedb2cf4bbe2913 100644 (file)
@@ -31,6 +31,7 @@ using System.Collections;
 using System.IO;
 using System.Security;
 using System.Security.Permissions;
+using System.Runtime.InteropServices;
 
 namespace System.CodeDom.Compiler {
 
@@ -45,6 +46,7 @@ namespace System.CodeDom.Compiler {
                bool keepfiles;
                string basepath;
                Random rnd;
+               string ownTempDir;
                
                public TempFileCollection ()
                        : this (String.Empty, false)
@@ -67,16 +69,15 @@ namespace System.CodeDom.Compiler {
                {
                        get {
                                if(basepath==null) {
-                                       // note: this property *cannot* change TempDir property
-                                       string temp = tempdir;
-                                       if (temp.Length == 0) {
-                                               // this call ensure the Environment permissions check
-                                               temp = Path.GetTempPath ();
-                                       }
-
+                               
                                        if (rnd == null)
                                                rnd = new Random ();
 
+                                       // note: this property *cannot* change TempDir property
+                                       string temp = tempdir;
+                                       if (temp.Length == 0)
+                                               temp = GetOwnTempDir ();
+
                                        // Create a temporary file at the target directory. This ensures
                                        // that the generated file name is unique.
                                        FileStream f = null;
@@ -111,6 +112,32 @@ namespace System.CodeDom.Compiler {
                                return(basepath);
                        }
                }
+               
+               string GetOwnTempDir ()
+               {
+                       if (ownTempDir != null)
+                               return ownTempDir;
+
+                       // this call ensure the Environment permissions check
+                       string basedir = Path.GetTempPath ();
+                       
+                       // Create a subdirectory with the correct user permissions
+                       int res = -1;
+                       do {
+                               int num = rnd.Next ();
+                               num++;
+                               ownTempDir = Path.Combine (basedir, num.ToString("x"));
+                               if (Directory.Exists (ownTempDir))
+                                       continue;
+                               res = mkdir (ownTempDir, 0x1c0);
+                               if (res != 0) {
+                                       if (!Directory.Exists (ownTempDir))
+                                               throw new IOException ();
+                                       // Somebody already created the dir, keep trying
+                               }
+                       } while (res != 0);
+                       return ownTempDir;
+               }
 
                int ICollection.Count {
                        get {
@@ -190,18 +217,25 @@ namespace System.CodeDom.Compiler {
                
                public void Delete()
                {
-                       string[] filenames=new string[filehash.Count];
-                       filehash.Keys.CopyTo(filenames, 0);
+                       bool allDeleted = true;
+                       string[] filenames = new string[filehash.Count];
+                       filehash.Keys.CopyTo (filenames, 0);
 
                        foreach(string file in filenames) {
                                if((bool)filehash[file]==false) {
                                        File.Delete(file);
                                        filehash.Remove(file);
-                               }
+                               } else
+                                       allDeleted = false;
                        }
                        if (basepath != null) {
                                string tmpFile = basepath + ".tmp";
                                File.Delete (tmpFile);
+                               basepath = null;
+                       }
+                       if (allDeleted && ownTempDir != null) {
+                               Directory.Delete (ownTempDir, true);
+                               ownTempDir = null;
                        }
                }
 
@@ -228,5 +262,6 @@ namespace System.CodeDom.Compiler {
                        Dispose(false);
                }
                
+               [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);
        }
 }