fix(appimage): DwarFS AppImages unpack without proot (#3161)

This commit is contained in:
The412Banner authored and GitHub committed 2026-11-24 17:50:04 -05:00
1 parent f934feb457
commit 43ff1db06b
12 files changed
+208 -1

No files matched your search

+55
View File
@@ -1090,6 +1090,61 @@ class ExtractTest(FexTestCase):
self.assertNotIn("no unsquashfs", said)
self.assertFalse(os.path.lexists("squashfs-root"))
def runtime_elf(self):
head = bytearray(elf(183))
struct.pack_into("<Q", head, 0x28, len(head))
return bytes(head)
def dwarfs(self, body):
self.write(self.image, self.runtime_elf() + b"DWARFS\x02\x05payload", 0o755)
self.ran = self.tmp / "dwarfsextract-ran"
return str(self.write(self.tmp / "dwarfsextract", "#!/bin/sh\ntouch %s\n%s" % (self.ran, body), 0o755))
def test_a_dwarfs_image_the_runtime_cannot_unpack_is_read_through_its_open_descriptor(self):
reader = self.dwarfs(
'[ "$1 $3 $4 $5" = "--input --image-offset=64 --output squashfs-root" ] || exit 9\n'
'case $2 in /proc/self/fd/*) ;; *) exit 8 ;; esac\n'
'cmp -s "$2" "%s" || exit 7\ntouch squashfs-root/AppRun\n' % self.image)
tool = self.uruntime("echo 'Failed to execute dwarfsextract: EACCES: Permission denied' >&2\nexit 1\n")
with mock.patch.dict(os.environ, {"DDNATV_ROOT": str(self.tmp / "rootfs")}), \
mock.patch.dict(G, {"URUNTIME": tool, "DWARFSEXTRACT": reader}), \
mock.patch("shutil.which", return_value=None):
status, said = self.quiet(FEX["extract"], "image.AppImage")
self.assertEqual(status, 0)
self.assertIn("uruntime could not extract it (status 1)", said)
self.assertNotIn("dwarfsextract could not", said)
self.assertTrue(Path("squashfs-root/AppRun").is_file())
self.assertFalse((self.home / ".config/droiddeck/fex-wanted").exists())
def test_a_dwarfs_image_the_runtime_unpacks_never_runs_dwarfsextract(self):
reader = self.dwarfs("touch squashfs-root/AppRun\n")
tool = self.uruntime("mkdir -p AppDir && touch AppDir/AppRun\n")
with mock.patch.dict(G, {"URUNTIME": tool, "DWARFSEXTRACT": reader}):
status, said = self.quiet(FEX["extract"], "image.AppImage")
self.assertEqual((status, said), (0, ""))
self.assertFalse(self.ran.exists())
def test_a_dwarfsextract_failure_leaves_nothing_unpacked(self):
reader = self.dwarfs("mkdir -p squashfs-root/usr\nexit 3\n")
tool = self.uruntime("exit 1\n")
with mock.patch.dict(G, {"URUNTIME": tool, "DWARFSEXTRACT": reader}), \
mock.patch("shutil.which", return_value=None):
status, said = self.quiet(FEX["extract"], "image.AppImage")
self.assertEqual(status, 1)
self.assertTrue(self.ran.exists())
self.assertIn("dwarfsextract could not extract it (status 3)", said)
self.assertFalse(os.path.lexists("squashfs-root"))
def test_a_squashfs_image_still_goes_to_unsquashfs(self):
reader = self.dwarfs("exit 0\n")
self.write(self.image, self.runtime_elf() + b"hsqs" + b"payload", 0o755)
tool = self.uruntime("exit 1\n")
with mock.patch.dict(G, {"DWARFSEXTRACT": reader}):
status, said, commands = self.seven_zip(tool, {"unsquashfs": "/usr/bin/unsquashfs", "7z": "/usr/bin/7z"})
self.assertEqual(status, 0)
self.assertNotIn("dwarfsextract", said)
self.assertEqual(["/usr/bin/unsquashfs", "/usr/bin/7z"], [c[0] for c in commands])
class LaunchTest(FexTestCase):
def test_the_guest_environment(self):
+45
View File
@@ -0,0 +1,45 @@
import struct
import subprocess
import tempfile
import unittest
from pathlib import Path
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/unwrap-dwarfs.sh"
class UnwrapDwarfsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.dir = Path(self.temp.name)
self.output = self.dir / "dwarfsextract"
def wrapper(self, payload, magic=b"SQUEEZE!", packed=None):
compressed = subprocess.run(["zstd", "-q", "-c"], input=payload, capture_output=True, check=True).stdout
size = len(compressed) if packed is None else packed
path = self.dir / "wrapper"
path.write_bytes(b"\x7fELF stub" * 100 + compressed + magic + struct.pack("<QQQ", len(payload), size, 0))
return path
def unwrap(self, wrapper):
return subprocess.run(["bash", str(SCRIPT), str(wrapper), str(self.output)], capture_output=True, text=True)
def test_the_payload_behind_the_stub_is_unpacked(self):
payload = b"\x7fELF" + bytes(range(256)) * 64
run = self.unwrap(self.wrapper(payload))
self.assertEqual(run.returncode, 0, run.stderr)
self.assertEqual(self.output.read_bytes(), payload)
def test_a_file_without_the_trailer_is_refused(self):
run = self.unwrap(self.wrapper(b"payload", magic=b"NOTSQZ!!"))
self.assertNotEqual(run.returncode, 0)
self.assertIn("no SQUEEZE! trailer", run.stderr)
def test_a_payload_size_beyond_the_file_is_refused(self):
run = self.unwrap(self.wrapper(b"payload", packed=1 << 40))
self.assertNotEqual(run.returncode, 0)
self.assertIn("bad payload size", run.stderr)
if __name__ == "__main__":
unittest.main()