diff --git a/app/src/main/java/com/droiddeck/launcher/core/ArchivePaths.java b/app/src/main/java/com/droiddeck/launcher/core/ArchivePaths.java new file mode 100644 index 0000000000..3f9eaee443 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/ArchivePaths.java @@ -0,0 +1,25 @@ +package com.droiddeck.launcher.core; + +import java.io.File; +import java.io.IOException; + +/** + * Where an archive entry may land. An entry name ("../x", "/etc/x") or a symlink already written + * by an earlier entry can point outside the directory being filled; the canonical path follows + * both, so anything that does not resolve inside it is refused. + */ +public final class ArchivePaths { + private ArchivePaths() {} + + /** + * The file for {@code name} under {@code destination}, or null when it would resolve outside + * it. The destination itself counts as inside (a "./" entry). + */ + public static File inside(File destination, String name) throws IOException { + String base = destination.getCanonicalPath(); + File file = new File(destination, name); + String path = file.getCanonicalPath(); + if (path.equals(base) || path.startsWith(base + File.separator)) return file; + return null; + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/core/TarZst.java b/app/src/main/java/com/droiddeck/launcher/core/TarZst.java index 0f0a71636c..0a1110f233 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/TarZst.java +++ b/app/src/main/java/com/droiddeck/launcher/core/TarZst.java @@ -37,11 +37,10 @@ public final class TarZst { destination.mkdirs(); try (InputStream in = new ZstdCompressorInputStream(new BufferedInputStream(source, 1 << 16)); TarArchiveInputStream tar = new TarArchiveInputStream(in)) { - String base = destination.getCanonicalPath() + File.separator; TarArchiveEntry entry; while ((entry = tar.getNextTarEntry()) != null) { - File file = new File(destination, entry.getName()); - if (!file.getCanonicalPath().startsWith(base)) continue; + File file = ArchivePaths.inside(destination, entry.getName()); + if (file == null) continue; if (entry.isDirectory()) { //noinspection ResultOfMethodCallIgnored file.mkdirs(); diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java index 119b89a98f..06290f8830 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java @@ -4,6 +4,7 @@ import android.content.Context; import android.util.Log; +import com.droiddeck.launcher.core.ArchivePaths; import com.droiddeck.launcher.core.Downloader; import com.droiddeck.launcher.core.FileUtils; @@ -284,12 +285,10 @@ public final class LinuxRuntimeInstaller { new BufferedInputStream(new FileInputStream(archive), 1 << 16)); TarArchiveInputStream tar = new TarArchiveInputStream(in)) { TarArchiveEntry entry; - String base = destination.getCanonicalPath() + File.separator; while ((entry = tar.getNextTarEntry()) != null) { - File file = new File(destination, entry.getName()); // Refuse anything that would land outside the runtime directory. - if (!(file.getCanonicalPath() + (entry.isDirectory() ? File.separator : "")).startsWith(base) - && !file.getCanonicalPath().equals(destination.getCanonicalPath())) { + File file = ArchivePaths.inside(destination, entry.getName()); + if (file == null) { Log.w(TAG, "skipping entry outside the rootfs: " + entry.getName()); continue; } @@ -306,7 +305,13 @@ public final class LinuxRuntimeInstaller { } else if (entry.isLink()) { // A hard link to an earlier entry. Link where the filesystem allows it and // fall back to a copy, which costs space but always works. - File target = new File(destination, entry.getLinkName()); + // The target is checked too: a link to "../../shared_prefs/x" would pull a + // file from outside the rootfs into it. + File target = ArchivePaths.inside(destination, entry.getLinkName()); + if (target == null) { + Log.w(TAG, "skipping hard link outside the rootfs: " + entry.getLinkName()); + continue; + } file.delete(); try { Files.createLink(file.toPath(), target.toPath());