From 05bfffa247c470d12a5b93326a05d3fd0d890ce0 Mon Sep 17 00:00:00 2001 From: Michael Barker Date: Sat, 3 Nov 2018 17:41:10 +0530 Subject: [PATCH 1/2] bz-62849 Check for filesystem loops due to symbolic links, in DirectoryScanner and Delete task --- src/etc/testcases/core/directoryscanner.xml | 5 +++++ .../org/apache/tools/ant/DirectoryScanner.java | 12 ++++++++++++ .../org/apache/tools/ant/taskdefs/Delete.java | 15 ++++++++++++++- .../apache/tools/ant/DirectoryScannerTest.java | 14 ++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/etc/testcases/core/directoryscanner.xml b/src/etc/testcases/core/directoryscanner.xml index 7e8683a4a..71eb88bf7 100644 --- a/src/etc/testcases/core/directoryscanner.xml +++ b/src/etc/testcases/core/directoryscanner.xml @@ -38,4 +38,9 @@ + + + + + diff --git a/src/main/org/apache/tools/ant/DirectoryScanner.java b/src/main/org/apache/tools/ant/DirectoryScanner.java index a0e7837e4..15caa4025 100644 --- a/src/main/org/apache/tools/ant/DirectoryScanner.java +++ b/src/main/org/apache/tools/ant/DirectoryScanner.java @@ -1258,6 +1258,18 @@ public class DirectoryScanner final String name = vpath + newFile; final TokenizedPath newPath = new TokenizedPath(path, newFile); final File file = new File(dir, newFile); + + try { + // check if it's a filesystem "loop" due to symbolic links + if (FileUtils.getFileUtils().isLeadingPath(file.getAbsoluteFile(), + dir.getAbsoluteFile(), true)) { + continue; + } + } catch (IOException e) { + System.err.println("Failed to determine if " + file + " causes a " + + "filesystem loop due to symbolic link; continuing"); + } + final String[] children = file.list(); if (children == null || (children.length == 0 && file.isFile())) { if (isIncluded(newPath)) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Delete.java b/src/main/org/apache/tools/ant/taskdefs/Delete.java index f887695b1..fdddbbf2d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Delete.java +++ b/src/main/org/apache/tools/ant/taskdefs/Delete.java @@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; @@ -771,7 +772,19 @@ public class Delete extends MatchingTask { } for (String s : list) { File f = new File(d, s); - if (f.isDirectory()) { + + boolean isFsLoop = false; + + try { + isFsLoop = SYMLINK_UTILS.isSymbolicLink(f) && + FileUtils.getFileUtils().isLeadingPath(f.getAbsoluteFile(), + d.getAbsoluteFile(), true); + } catch (IOException e) { + log("Failed to check if " + f + " causes a filesystem loop due to " + + "symbolic link; continuing"); + } + + if (f.isDirectory() && !isFsLoop) { removeDir(f); } else { log("Deleting " + f.getAbsolutePath(), quiet ? Project.MSG_VERBOSE : verbosity); diff --git a/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java b/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java index 819d4e50e..2abd94f31 100644 --- a/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java +++ b/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java @@ -132,6 +132,20 @@ public class DirectoryScannerTest { new String[] {"alpha/beta/gamma"}); } + @Test + public void testAllowRecursiveSymlinks() { + + assumeTrue("Current system does not support Symlinks", supportsSymlinks); + + buildRule.getProject().executeTarget("symlink-nested-setup"); + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(new File(buildRule.getProject().getProperty("output"))); + ds.setIncludes(new String[] {"alpha/beta/gamma/"}); + ds.scan(); + compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"}, + new String[] {"alpha/beta/gamma"}); + } + @Test public void testProhibitSymlinks() { assumeTrue("Current system does not support Symlinks", supportsSymlinks); From 506c3ab47a1d69ecff691cc535ae8368d0aeb1db Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Sat, 3 Nov 2018 17:48:47 +0530 Subject: [PATCH 2/2] Record the fix for bz-62849, contributed by Michael Barker --- CONTRIBUTORS | 1 + WHATSNEW | 5 +++++ contributors.xml | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7005c7562..7d4a3fd69 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -263,6 +263,7 @@ Matthew Kuperus Heun Matthew Watson Matthew Yanos Matthias Bhend +Michael Barker Michael Bayne Michael Clarke Michael Davey diff --git a/WHATSNEW b/WHATSNEW index e5bfed371..ab6d0cdad 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -16,6 +16,11 @@ Fixed bugs: an incorrect compression level for a zip entry. This is now fixed. Bugzilla Report 62686 + * A filesystem "loop" caused due to symbolic links could trigger an + out of memory error in the org.apache.tools.ant.DirectoryScanner + This has now been fixed. + Bugzilla Report 62849 + Other changes: -------------- * generatekey task now supports SubjectAlternativeName during key diff --git a/contributors.xml b/contributors.xml index a62b1d477..88b7a8c1e 100644 --- a/contributors.xml +++ b/contributors.xml @@ -1073,6 +1073,10 @@ Matthew Yanos + + Michael + Barker + Michael Bayne