From 3e0ddf70e94fdd0e3cdadbbed31bdb5f63714c16 Mon Sep 17 00:00:00 2001 From: tkchia Date: Sun, 2 Oct 2022 15:45:25 +0800 Subject: [PATCH] Make shell script magic less prone to breakage (#643) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The metal boot sector code was wrapped in a single-quoted string, like so: MZqFpD=' ...MZ HEADER... ...E_LFANEW... ...BIOS BOOT SECTOR...' This might break the shell code loader if the boot sector code contains a 0x27 (single quote) byte. This patch wraps the boot sector code in a here-document instead: MZqFpD=' ...MZ HEADER...' <<'@' ...E_LFANEW... ...BIOS BOOT SECTOR... @ This is harder to break — when interpreted as a shell script, the code can only accidentally terminate the here-document if there is a "\n@\n" sequence inside the BIOS boot portion. --- ape/ape.S | 5 ++--- ape/ape.lds | 5 +---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ape/ape.S b/ape/ape.S index 7728a9fc2..72dbc8115 100644 --- a/ape/ape.S +++ b/ape/ape.S @@ -125,6 +125,7 @@ ape_mz: .asciz "MZqFpD='\n" # Mark 'Zibo' Joseph Zbikowski .org 0x24 # MZ: bytes reserved for you .ascii "JT" # MZ: OEM identifier .short 0 # MZ: OEM information + .ascii "' <<'@'\n" .org 0x40-4 # MZ: bytes reserved for you #if SupportsWindows() || SupportsMetal() .long RVA(ape_pe) # PE: the new technology @@ -250,7 +251,6 @@ pc: cld xor %cx,%cx # current cylinder xor %dh,%dh # current head mov $v_ape_realsectors,%di # total sectors - sub $v_ape_realslacksectors,%di 3: call pcread dec %di jnz 3b @@ -549,7 +549,7 @@ ape_disk: the bourne executable & linkable format */ #if SupportsWindows() || SupportsMetal() || SupportsXnu() -apesh: .ascii "'\n#'\"\n" # sixth edition shebang +apesh: .ascii "\n@\n#'\"\n" # sixth edition shebang // Until all operating systems can be updated to support APE, // we have a beautiful, yet imperfect workaround, which is to // modify the binary to follow the local system's convention. @@ -1597,7 +1597,6 @@ kernel: movabs $ape_stack_vaddr,%rsp .ldsvar _end .ldsvar _etext .ldsvar v_ape_realsectors - .ldsvar v_ape_realslacksectors .ldsvar v_ape_highsectors .ldsvar ape_idata_ro .ldsvar ape_pad_rodata diff --git a/ape/ape.lds b/ape/ape.lds index 24e233b62..08c5860b0 100644 --- a/ape/ape.lds +++ b/ape/ape.lds @@ -565,10 +565,7 @@ SHSTUB2(ape_loader_dd_count, #if SupportsMetal() HIDDEN(v_ape_realsectors = MIN(0x70000 - IMAGE_BASE_REAL, - ROUNDUP(RVA(_edata), 4096)) / 512); -HIDDEN(v_ape_realslacksectors = - v_ape_realsectors - MIN(0x70000 - IMAGE_BASE_REAL, - ROUNDUP(RVA(_edata), 512)) / 512); + ROUNDUP(RVA(_edata), 512)) / 512); HIDDEN(v_ape_realpages = v_ape_realsectors / (4096 / 512)); HIDDEN(v_ape_highsectors = (ROUNDUP(RVA(_edata), 512) / 512) - v_ape_realsectors);