From b46ac13504e991910127aa0ffe56a6983ffd1ce2 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 10 Nov 2022 20:58:48 -0800 Subject: [PATCH] Introduce new `jartsr='` ape prefix Files beginning with "MZqFpD" are Actually Portable Executables, which have a printf statement in the first 4096 bytes with octal codes that specify the ELF header. APE also specifies `jartsr='` as an alternative prefix, intended for binaries that do not want to be identified as Windows executables. Like the \177ELF magic, all these prefixes decode as x86 jump instructions that could be used for 16-bit bootloaders or 32-bit / 64-bit flat executables. Most importantly they provide a fallback path for Thompson shell copmatible command interpreters, which do not require a shebang, e.g. bash, zsh, fish, bourne, almquist, etc. Please note that in order to meet the requirements of POSIX.1, the single quote must be followed by a newline character, before any null bytes occur. See also: https://www.austingroupbugs.net/view.php?id=1250 --- ape/ape.S | 27 ++++++++++++++++++++++++--- ape/apeinstall.sh | 4 ++-- ape/apeuninstall.sh | 18 ++++++++---------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/ape/ape.S b/ape/ape.S index 412a03626..234b5d07d 100644 --- a/ape/ape.S +++ b/ape/ape.S @@ -116,11 +116,29 @@ cstr: .endobj cstr,globl,hidden # ←for gdb readability ape_mz: #if SupportsWindows() || SupportsMetal() .asciz "MZqFpD='\n" # Mark 'Zibo' Joseph Zbikowski +// # in real mode +// dec %bp +// pop %dx +// jno 0x4a +// jo 0x4a +// +// # in legacy mode +// push %ebp +// pop %edx +// jno 0x4a +// jo 0x4a +// +// # 64-bit mode +// rex.WRB +// pop %r10 +// jno 0x4a +// jo 0x4a #else // Avoid virus scanner reputation damage when targeting System Five. -// WARNING: This prefix is experimental; it may be removed sometime. -// TODO(jart): Find another prefix that will work with BIOS loading. - .asciz "JTqFpD='\n" # Mark 'Zibo' Joseph Zbikowski + .asciz "jartsr='\n" # Justine Alexandra Roberts Tunney +// push $0x61 +// jb 0x78 +// jae 0x78 #endif .short 0x1000 # MZ: lowers upper bound load / 16 .short 0xf800 # MZ: roll greed on bss @@ -140,6 +158,9 @@ ape_mz: .long RVA(ape_pe) # PE: the new technology #else .long 0 + .org 0x78 + pop %rax + jmp _start #endif .endfn ape_mz,globl,hidden diff --git a/ape/apeinstall.sh b/ape/apeinstall.sh index c97d8fff7..334ed2778 100755 --- a/ape/apeinstall.sh +++ b/ape/apeinstall.sh @@ -97,8 +97,8 @@ if [ x"$(uname -s)" = xLinux ]; then echo you may need to edit configs to persist across reboot >&2 echo '$SUDO sh -c "echo '"'"':APE:M::MZqFpD::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2 $SUDO sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit - echo '$SUDO sh -c "echo '"'"':APE-sysv:M::JTqFpD::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2 - $SUDO sh -c "echo ':APE-sysv:M::JTqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit + echo '$SUDO sh -c "echo '"'"':APE-jart:M::jartsr::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2 + $SUDO sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit echo done >&2 if [ x"$(cat /proc/sys/fs/binfmt_misc/status)" = xdisabled ]; then diff --git a/ape/apeuninstall.sh b/ape/apeuninstall.sh index 69cd41031..5cad154a8 100755 --- a/ape/apeuninstall.sh +++ b/ape/apeuninstall.sh @@ -8,21 +8,19 @@ fi { echo - echo "APE Uninstaller intends to run" + echo "APE Uninstaller intends to run (in pseudo-shell)" echo - echo " $SUDO sh -c 'echo -1 >/proc/sys/fs/binfmt_misc/APE'" - echo " $SUDO sh -c 'echo -1 >/proc/sys/fs/binfmt_misc/APE-sysv'" - echo " $SUDO rm -f /usr/bin/ape ~/.ape o/tmp/.ape /tmp/.ape" + echo " sudo echo -1 into /proc/sys/fs/binfmt_misc/APE*" + echo " sudo rm -f /usr/bin/ape ~/.ape o/tmp/.ape /tmp/.ape" echo echo "You may then use ape/apeinstall.sh to reinstall it" echo } >&2 set -ex -if [ -f /proc/sys/fs/binfmt_misc/APE ]; then - $SUDO sh -c 'echo -1 >/proc/sys/fs/binfmt_misc/APE' || exit -fi -if [ -f /proc/sys/fs/binfmt_misc/APE-sysv ]; then - $SUDO sh -c 'echo -1 >/proc/sys/fs/binfmt_misc/APE-sysv' || exit -fi +for f in /proc/sys/fs/binfmt_misc/APE*; do + if [ -f $f ]; then + $SUDO sh -c "echo -1 >$f" || exit + fi +done $SUDO rm -f /usr/bin/ape ~/.ape o/tmp/.ape o/tmp/ape /tmp/.ape /tmp/ape || exit