Add binfmt_misc workaround detector to builds

This is a recently introduced Linux Kernel feature that gives people
like Debian package mantainers the power to arbitrarily redefine how
executables are interpreted by the kernel. If your system gets tuned
this way and you're not able to disable it, then you need to restore
default behavior for the APE MZqFpD prefix as follows:

    sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"

This prefix will cover all .com executables built with this tooling.
Please don't run the above command unless you're certain you need it.
See #2 for additional context.
main
Justine Tunney 2020-08-31 19:31:11 -07:00
parent bd29223891
commit f213556366
24 changed files with 89 additions and 17 deletions

View File

@ -60,8 +60,9 @@
#
# build/config.mk
SHELL = /bin/sh
HOSTS ?= freebsd openbsd alpine
SHELL = /bin/sh
HOSTS ?= freebsd openbsd alpine
SANITY := $(shell build/sanitycheck $$PPID)
.SUFFIXES:
.DELETE_ON_ERROR:

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
# -*- mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8 -*-
# vi: set net ft=sh ts=2 sts=2 sw=2 fenc=utf-8 :vi

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/usr/bin/env bash
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

57
build/sanitycheck 100755
View File

@ -0,0 +1,57 @@
#!/bin/sh
#
# OVERVIEW
#
# System Sanity Check
#
# DESCRIPTION
#
# This script is launched at the start of Makefile to detect if
# binfmt_misc was tuned to launch 'MZ' shell scripts under WINE
if [ x`uname -s` != xLinux ]; then
cat <<'EOF' >&2
ERROR
Want Linux Build Environment
DETAILS
Cosmopolitan builds binaries that run on all major platforms.
You need to compile them on Linux, using any distro you like.
Consider setting up Alpine, Debian, or Ubuntu in a VMWare VM.
EOF
kill $1
exit 1
fi
build/sanitycheck2
if [ $? -ne 123 ]; then
cat <<'EOF' >&2
ERROR
Thompson Shell Backwards Compatibility Issue Detected
DETAILS
Actually Portable Executable assumes stock Linux configuration.
Normal behavior is non-ELF files with x bit are run by /bin/sh.
Linux lets people globally define arbitrary magic interpreters.
Your computer couldve been tuned to run MZ scripts inside WINE.
So if you use binfmt_misc you need to explicitly register this.
WORKAROUND
sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"
SEE ALSO
https://justine.storage.googleapis.com/ape.html
EOF
kill $1
exit 1
fi

View File

@ -0,0 +1,9 @@
PE=123
exit $PE

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
#

View File

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘

View File

@ -110,21 +110,6 @@ static void showmzheader(void) {
}
static void showdosstub(void) {
unsigned char *p = (unsigned char *)mz + sizeof(struct NtImageDosHeader);
unsigned char *pe = (mz->e_lfanew ? p + mz->e_lfanew : p + mzsize);
pe = min(pe, p + mzsize - XED_MAX_INSTRUCTION_BYTES);
while (p < pe) {
struct XedDecodedInst *inst = ildreal(p);
if (p + inst->length > pe) break;
printf("\t.byte\t");
for (unsigned i = 0; i < inst->length; ++i) {
if (i) printf(",");
printf("%#hhx", inst->bytes[i]);
}
printf("\n");
p += inst->length;
}
printf("\n");
}
static void showpeoptionalheader(struct NtImageOptionalHeader *opt) {