Skip to content

Commit

Permalink
Safeguard classpath/module-path against symlinks to identical entries
Browse files Browse the repository at this point in the history
  • Loading branch information
olpaw committed Sep 6, 2022
1 parent 96d195f commit 63e0b87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -1444,7 +1443,7 @@ Path canonicalize(Path path, boolean strict) {
absolutePath = absolutePath.getParent();
}
try {
Path realPath = absolutePath.toRealPath(LinkOption.NOFOLLOW_LINKS);
Path realPath = absolutePath.toRealPath();
if (!Files.isReadable(realPath)) {
showError("Path entry " + ClasspathUtils.classpathToString(path) + " is not readable");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,18 @@ protected NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, St
}
buildcp = Arrays.stream(builderClassPathEntries)
.map(Path::of)
.map(Path::toAbsolutePath)
.map(Util::toRealPath)
.collect(Collectors.toUnmodifiableList());

imagemp = Arrays.stream(modulePath)
.map(Path::of)
.map(Util::toRealPath)
.collect(Collectors.toUnmodifiableList());

buildmp = Optional.ofNullable(System.getProperty("jdk.module.path")).stream()
.flatMap(s -> Arrays.stream(s.split(File.pathSeparator)))
.map(Path::of)
.map(Util::toRealPath)
.collect(Collectors.toUnmodifiableList());

upgradeAndSystemModuleFinder = createUpgradeAndSystemModuleFinder();
Expand Down Expand Up @@ -216,13 +218,21 @@ static URL[] verifyClassPathAndConvertToURLs(String[] classpath) {
Stream<Path> pathStream = new LinkedHashSet<>(Arrays.asList(classpath)).stream().flatMap(Util::toClassPathEntries);
return pathStream.map(v -> {
try {
return v.toAbsolutePath().toUri().toURL();
return toRealPath(v).toUri().toURL();
} catch (MalformedURLException e) {
throw UserError.abort("Invalid classpath element '%s'. Make sure that all paths provided with '%s' are correct.", v, SubstrateOptions.IMAGE_CLASSPATH_PREFIX);
}
}).toArray(URL[]::new);
}

static Path toRealPath(Path p) {
try {
return p.toRealPath();
} catch (IOException e) {
throw UserError.abort("Path entry '%s' does not map to a real path.", p);
}
}

static Stream<Path> toClassPathEntries(String classPathEntry) {
Path entry = ClasspathUtils.stringToClasspath(classPathEntry);
if (entry.endsWith(ClasspathUtils.cpWildcardSubstitute)) {
Expand Down

0 comments on commit 63e0b87

Please sign in to comment.