diff --git a/modules/kernel/src/org/apache/axis2/context/ConfigurationContext.java b/modules/kernel/src/org/apache/axis2/context/ConfigurationContext.java index 1365a582f0..4a5db4307c 100644 --- a/modules/kernel/src/org/apache/axis2/context/ConfigurationContext.java +++ b/modules/kernel/src/org/apache/axis2/context/ConfigurationContext.java @@ -41,6 +41,8 @@ import java.io.File; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.LinkOption; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Date; @@ -799,10 +801,12 @@ public String run() { } private void deleteTempFiles(final File dir) { + // Do not descend through a symbolic link: the temp directory can sit in a + // shared location, and following a link would delete its target's contents. Boolean isDir = AccessController.doPrivileged( new PrivilegedAction() { public Boolean run() { - return dir.isDirectory(); + return Files.isDirectory(dir.toPath(), LinkOption.NOFOLLOW_LINKS); } } ); diff --git a/modules/kernel/src/org/apache/axis2/deployment/util/TempFileManager.java b/modules/kernel/src/org/apache/axis2/deployment/util/TempFileManager.java index 3517a065af..7a9164de58 100644 --- a/modules/kernel/src/org/apache/axis2/deployment/util/TempFileManager.java +++ b/modules/kernel/src/org/apache/axis2/deployment/util/TempFileManager.java @@ -24,6 +24,8 @@ import java.io.FileFilter; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.LinkOption; import java.util.logging.Level; import java.util.logging.Logger; @@ -143,8 +145,9 @@ private static void recursiveDelete(File rootDir) if (files != null) { for (File file : files) { // If the file is a directory, we will - // recursively call delete on it. - if (file.isDirectory()) { + // recursively call delete on it. A symbolic link is + // deleted itself and never followed. + if (Files.isDirectory(file.toPath(), LinkOption.NOFOLLOW_LINKS)) { recursiveDelete(file); } else { // It is just a file so we are safe to @@ -183,7 +186,7 @@ private static void recursiveDelete(File rootDir) FileFilter tmpDirFilter = new FileFilter() { public boolean accept(File pathname) { - return (pathname.isDirectory() && + return (Files.isDirectory(pathname.toPath(), LinkOption.NOFOLLOW_LINKS) && pathname.getName().startsWith(TEMP_DIR_PREFIX)); } }; diff --git a/modules/kernel/test/org/apache/axis2/context/ConfigurationContextTempCleanupTest.java b/modules/kernel/test/org/apache/axis2/context/ConfigurationContextTempCleanupTest.java new file mode 100644 index 0000000000..98aaaf2b1c --- /dev/null +++ b/modules/kernel/test/org/apache/axis2/context/ConfigurationContextTempCleanupTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.context; + +import org.apache.axis2.Constants; + +import junit.framework.TestCase; + +import java.io.File; +import java.nio.file.Files; + +public class ConfigurationContextTempCleanupTest extends TestCase { + + /** + * Removing the artifacts temp directory on terminate must delete a symbolic + * link inside it, not the contents of the link's target. + */ + public void testTerminateDoesNotFollowSymlinksInTempDir() throws Exception { + File base = Files.createTempDirectory("axis2-cc").toFile(); + File artifacts = new File(base, "_axis2"); + File victim = new File(base, "victim"); + assertTrue(artifacts.mkdir()); + assertTrue(victim.mkdir()); + File kept = new File(victim, "kept.txt"); + Files.write(kept.toPath(), new byte[] { 1 }); + try { + Files.createSymbolicLink(new File(artifacts, "link").toPath(), victim.toPath()); + } catch (UnsupportedOperationException | java.io.IOException e) { + return; // no symlink support on this platform + } + + ConfigurationContext configContext = + ConfigurationContextFactory.createEmptyConfigurationContext(); + configContext.getAxisConfiguration().addParameter( + Constants.Configuration.ARTIFACTS_TEMP_DIR, artifacts); + configContext.terminate(); + + assertTrue("symlink target was emptied", kept.exists()); + assertFalse("artifacts temp dir was not removed", artifacts.exists()); + } +} diff --git a/modules/kernel/test/org/apache/axis2/deployment/util/TempFileManagerTest.java b/modules/kernel/test/org/apache/axis2/deployment/util/TempFileManagerTest.java new file mode 100644 index 0000000000..bdd5eb98e4 --- /dev/null +++ b/modules/kernel/test/org/apache/axis2/deployment/util/TempFileManagerTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.deployment.util; + +import junit.framework.TestCase; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; + +public class TempFileManagerTest extends TestCase { + + /** + * The stale-directory sweep that runs when TempFileManager is loaded must + * not follow a symbolic link planted in the shared temp directory. + */ + public void testStaleDirectoryCleanupDoesNotFollowSymlinks() throws Exception { + File base = Files.createTempDirectory("axis2-tfm").toFile(); + File tmp = new File(base, "tmp"); + File victim = new File(base, "victim"); + assertTrue(tmp.mkdir()); + assertTrue(new File(victim, "sub").mkdirs()); + File kept = new File(victim, "sub/kept.txt"); + Files.write(kept.toPath(), new byte[] { 1 }); + + File topLink = new File(tmp, "axis2-tmp-link"); + File stale = new File(tmp, "axis2-tmp-stale"); + assertTrue(stale.mkdir()); + File innerLink = new File(stale, "inner"); + try { + Files.createSymbolicLink(topLink.toPath(), victim.toPath()); + Files.createSymbolicLink(innerLink.toPath(), victim.toPath()); + } catch (UnsupportedOperationException | java.io.IOException e) { + return; // no symlink support on this platform + } + + // Load a fresh copy of the class so its static cleanup runs against tmp. + URL classes = TempFileManager.class.getProtectionDomain().getCodeSource().getLocation(); + String previous = System.getProperty("java.io.tmpdir"); + System.setProperty("java.io.tmpdir", tmp.getAbsolutePath()); + try (URLClassLoader loader = new URLClassLoader(new URL[] { classes }, + ClassLoader.getPlatformClassLoader())) { + Class.forName(TempFileManager.class.getName(), true, loader); + } finally { + System.setProperty("java.io.tmpdir", previous); + } + + assertTrue("symlink target was emptied", kept.exists()); + assertFalse("stale directory was not removed", stale.exists()); + assertTrue("top-level symlink was treated as a temp dir", Files.isSymbolicLink(topLink.toPath())); + } +}