diff --git a/tools/linuxfs/preload/binfmt.c b/tools/linuxfs/preload/binfmt.c index 94cecdeb86..50b5dd616f 100644 --- a/tools/linuxfs/preload/binfmt.c +++ b/tools/linuxfs/preload/binfmt.c @@ -32,17 +32,26 @@ static int handing_off(void) { return flag("DD_DESKTOP") || flag("DD_BINFMT"); } +static int appimage(int fd, const unsigned char *head, ssize_t n) { + Elf64_Ehdr eh; + if (n < (ssize_t)sizeof eh || head[EI_CLASS] != ELFCLASS64 || memcmp(head + EI_ABIVERSION, "AI\x02", 3)) return 0; + memcpy(&eh, head, sizeof eh); + char magic[6]; + off_t at = (off_t)eh.e_shoff + (off_t)eh.e_shentsize * eh.e_shnum; + return pread(fd, magic, sizeof magic, at) == (ssize_t)sizeof magic && (!memcmp(magic, "hsqs", 4) || !memcmp(magic, "DWARFS", 6)); +} + static int foreign(const char *path, int error) { if (!path || !strcmp(path, BINFMT_HANDLER)) return 0; int fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) return 0; struct stat st; - unsigned char head[20]; + unsigned char head[sizeof(Elf64_Ehdr)]; ssize_t n = fstat(fd, &st) == 0 && S_ISREG(st.st_mode) ? read(fd, head, sizeof head) : -1; int kind = 0; if (n >= 20 && !memcmp(head, ELFMAG, SELFMAG)) { unsigned machine = head[EI_DATA] == ELFDATA2MSB ? (unsigned)head[18] << 8 | head[19] : head[18] | (unsigned)head[19] << 8; - kind = machine == EM_X86_64 || machine == EM_386 || error == EACCES ? 1 : 0; + kind = machine == EM_X86_64 || machine == EM_386 || appimage(fd, head, n) || error == EACCES ? 1 : 0; } else if (n >= 2 && head[0] == 'M' && head[1] == 'Z') { kind = 2; } else if (n >= 2 && head[0] == '#' && head[1] == '!' && error == EACCES) { diff --git a/tools/tests/test_binfmt_preload.py b/tools/tests/test_binfmt_preload.py index e207b5c9ca..04b21b6021 100644 --- a/tools/tests/test_binfmt_preload.py +++ b/tools/tests/test_binfmt_preload.py @@ -138,6 +138,27 @@ class BinfmtPreloadTest(unittest.TestCase): program = self.file("arm64", bytes(arm64)) self.assertEqual(self.run_program("execve", program), (3, "errno %d" % errno.ENOEXEC, None)) + def arm64_appimage(self, name, filesystem): + image = bytearray(128) + image[0:4], image[4], image[5], image[8:11], image[18] = b"\x7fELF", 2, 1, b"AI\x02", 183 + image[0x28:0x30] = (64).to_bytes(8, "little") + image[0x3A:0x3C], image[0x3C:0x3E] = (32).to_bytes(2, "little"), (1).to_bytes(2, "little") + image[96:102] = filesystem + return self.file(name, bytes(image)) + + def test_an_arm64_appimage_goes_to_the_handler(self): + program = self.arm64_appimage("App.AppImage", b"hsqs\0\0") + for how in ("execve", "execvp"): + self.log.unlink(missing_ok=True) + target = program if how == "execve" else program.name + self.assertEqual(self.run_program(how, target, "--x"), (7, "", ["--exec", str(program), "--x"])) + self.log.unlink() + self.assertEqual(self.run_program("spawn", program, "y"), (0, "exit 7", ["--exec", str(program), "y"])) + + def test_an_appimage_runtime_without_a_filesystem_keeps_its_error(self): + runtime = self.arm64_appimage("uruntime", bytes(6)) + self.assertEqual(self.run_program("execve", runtime), (3, "errno %d" % errno.ENOEXEC, None)) + def test_a_script_runs_as_itself(self): script = self.file("script.sh", b"#!/bin/sh\nexit 5\n") self.assertEqual(self.run_program("execve", script), (5, "", None))