diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index deaac70142ce49f371940422749af048312e833a..5e4459bc40eba9bf11c84f4c1b1c0db6267075c1 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -2074,6 +2074,14 @@ class SimpleSAML_Utilities {
 				throw new SimpleSAML_Error_Exception('Error creating temp dir ' .
 					var_export($tempDir, TRUE) . ': ' . SimpleSAML_Utilities::getLastError());
 			}
+		} elseif (function_exists('posix_getuid')) {
+
+			/* Check that the owner of the temp diretory is the current user. */
+			$stat = lstat($tempDir);
+			if ($stat['uid'] !== posix_getuid()) {
+				throw new SimpleSAML_Error_Exception('Temp directory (' . var_export($tempDir, TRUE) .
+					') not owned by current user.');
+			}
 		}
 
 		return $tempDir;