]> granicus.if.org Git - python/commitdiff
Security patch for Unix by Chris McDonough.
authorGuido van Rossum <guido@python.org>
Mon, 24 Apr 2000 13:28:02 +0000 (13:28 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 24 Apr 2000 13:28:02 +0000 (13:28 +0000)
This uses the same precautions when trying to find a temporary
directory as when the actual tempfile is created (using O_CREAT and
O_EXCL).  On non-posix platforms, nothing is changed.

Lib/tempfile.py

index 5b05bdd1b365f572da54b8a3f1a7701b68270f86..eef6bffe11ed4adbf284704bdcfa8383624ac482 100644 (file)
@@ -42,13 +42,27 @@ def gettempdir():
     testfile = gettempprefix() + 'test'
     for dir in attempdirs:
         try:
-            filename = os.path.join(dir, testfile)
-            fp = open(filename, 'w')
-            fp.write('blat')
-            fp.close()
-            os.unlink(filename)
-            tempdir = dir
-            break
+           filename = os.path.join(dir, testfile)
+           if os.name == 'posix':
+               try:
+                   fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
+               except OSError:
+                   pass
+               else:
+                   fp = os.fdopen(fd, 'w')
+                   fp.write('blat')
+                   fp.close()
+                   os.unlink(filename)
+                   del fp, fd
+                   tempdir = dir
+                   break
+           else:
+               fp = open(filename, 'w')
+               fp.write('blat')
+               fp.close()
+               os.unlink(filename)
+               tempdir = dir
+               break
         except IOError:
             pass
     if tempdir is None: