Skip to content

Commit

Permalink
Backport systemd unreadable library path fix (#109419)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
thecoop and rjernst authored Jun 6, 2024
1 parent aba35e1 commit 4aa3340
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/108931.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 108931
summary: Guard systemd library lookup from unreadable directories
area: Infra/Core
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandle;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -61,17 +65,36 @@ static List<String> findLibSystemd() {
// so we must manually check the library path to find what we need.
final Path libsystemd = Paths.get("libsystemd.so.0");
final String libpath = System.getProperty("java.library.path");
return Arrays.stream(libpath.split(":")).map(Paths::get).filter(Files::exists).flatMap(p -> {
final List<String> foundPaths = new ArrayList<>();
Arrays.stream(libpath.split(":")).map(Paths::get).filter(Files::exists).forEach(rootPath -> {
try {
return Files.find(
p,
Integer.MAX_VALUE,
(fp, attrs) -> (attrs.isDirectory() == false && fp.getFileName().equals(libsystemd))
);
Files.walkFileTree(rootPath, new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (Files.isReadable(dir)) {
return FileVisitResult.CONTINUE;
}
return FileVisitResult.SKIP_SUBTREE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().equals(libsystemd)) {
foundPaths.add(file.toAbsolutePath().toString());
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}).map(p -> p.toAbsolutePath().toString()).toList();
});
return foundPaths;
}

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

0 comments on commit 4aa3340

Please sign in to comment.