Skip to content

Commit 4aa3340

Browse files
thecooprjernst
andauthored
Backport systemd unreadable library path fix (#109419)
* Guard systemd library lookup from unreadable directories (#108931) When scanning the library path we may come across directories that are unreadable. If that happens, the recursive walk of the library path directories will throw a fatal IOException. This commit guards the walk of the library paths to first check for readability of each directory we are about to traverse. --------- Co-authored-by: Ryan Ernst <[email protected]>
1 parent aba35e1 commit 4aa3340

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

docs/changelog/108931.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 108931
2+
summary: Guard systemd library lookup from unreadable directories
3+
area: Infra/Core
4+
type: bug
5+
issues: []

libs/native/src/main21/java/org/elasticsearch/nativeaccess/jdk/JdkSystemdLibrary.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
import java.lang.foreign.FunctionDescriptor;
1717
import java.lang.foreign.MemorySegment;
1818
import java.lang.invoke.MethodHandle;
19+
import java.nio.file.FileVisitResult;
1920
import java.nio.file.Files;
2021
import java.nio.file.Path;
2122
import java.nio.file.Paths;
23+
import java.nio.file.SimpleFileVisitor;
24+
import java.nio.file.attribute.BasicFileAttributes;
25+
import java.util.ArrayList;
2226
import java.util.Arrays;
2327
import java.util.List;
2428

@@ -61,17 +65,36 @@ static List<String> findLibSystemd() {
6165
// so we must manually check the library path to find what we need.
6266
final Path libsystemd = Paths.get("libsystemd.so.0");
6367
final String libpath = System.getProperty("java.library.path");
64-
return Arrays.stream(libpath.split(":")).map(Paths::get).filter(Files::exists).flatMap(p -> {
68+
final List<String> foundPaths = new ArrayList<>();
69+
Arrays.stream(libpath.split(":")).map(Paths::get).filter(Files::exists).forEach(rootPath -> {
6570
try {
66-
return Files.find(
67-
p,
68-
Integer.MAX_VALUE,
69-
(fp, attrs) -> (attrs.isDirectory() == false && fp.getFileName().equals(libsystemd))
70-
);
71+
Files.walkFileTree(rootPath, new SimpleFileVisitor<>() {
72+
@Override
73+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
74+
if (Files.isReadable(dir)) {
75+
return FileVisitResult.CONTINUE;
76+
}
77+
return FileVisitResult.SKIP_SUBTREE;
78+
}
79+
80+
@Override
81+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
82+
if (file.getFileName().equals(libsystemd)) {
83+
foundPaths.add(file.toAbsolutePath().toString());
84+
}
85+
return FileVisitResult.CONTINUE;
86+
}
87+
88+
@Override
89+
public FileVisitResult visitFileFailed(Path file, IOException exc) {
90+
return FileVisitResult.CONTINUE;
91+
}
92+
});
7193
} catch (IOException e) {
7294
throw new UncheckedIOException(e);
7395
}
74-
}).map(p -> p.toAbsolutePath().toString()).toList();
96+
});
97+
return foundPaths;
7598
}
7699

77100
private static final MethodHandle sd_notify$mh = downcallHandle("sd_notify", FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS));

0 commit comments

Comments
 (0)