#!/bin/bash
# busybox.SlackBuild -- AM·OS
# Static PIE busybox for the installer initrd. Fully hardened:
# canary, FORTIFY=3, stack-clash, CET, RELRO, noexecstack, ASLR.
#
# Busybox mainline does not support CONFIG_STATIC=y + CONFIG_PIE=y
# together (its Kconfig has `depends on !STATIC` on PIE, and its
# Makefile.flags passes -static / -pie as the last CFLAGS_busybox
# entry, so whichever comes second wins). The upstream patch applied
# below removes the Kconfig constraint and routes -static-pie only
# to the final link, not to partial `ld -r` links (which would fail
# with "-r and -pie may not be used together").
#
# make_world.sh's generic hardening pass skips this file; hardening
# is applied explicitly below.

set -eu

PKGNAM=busybox
VERSION=${VERSION:-1.37.0}
BUILD=${BUILD:-1}
ARCH=${ARCH:-x86_64}
TAG=${TAG:-amos}

if [ "${PRINT_PACKAGE_NAME:-}" = "true" ]; then
  echo "${PKGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txz"
  exit 0
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-$CWD/../../../amos64/extra}
SRCDIR="${SRCDIR:-$CWD}"

rm -rf "$PKG"
mkdir -p "$TMP" "$PKG" "$OUTPUT"
cd "$TMP"
rm -rf "$PKGNAM-$VERSION"
tar xf "$SRCDIR/$PKGNAM-$VERSION.tar.bz2"
cd "$PKGNAM-$VERSION"
chown -R root:root .

# ---- Apply static-pie support patch ----
cat > "$SRCDIR/static-pie.patch" <<'PATCH_EOF'
--- a/Config.in
+++ b/Config.in
@@ -390,7 +390,6 @@ config STATIC
 config PIE
 	bool "Build position independent executable"
 	default n
-	depends on !STATIC
 	help
 	  Hardened code option. PIE binaries are loaded at a different
 	  address at each invocation. This has some overhead,
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -108,6 +108,7 @@ endif
 ARCH_FPIC ?= -fpic
 ARCH_FPIE ?= -fpie
 ARCH_PIE ?= -pie
+ARCH_SPIE ?= -static-pie
 
 # Usage: $(eval $(call pkg_check_modules,VARIABLE-PREFIX,MODULES))
 define pkg_check_modules
@@ -125,13 +126,20 @@ CFLAGS += $(call cc-option,-fvisibility=hidden)
 endif
 
 ifeq ($(CONFIG_STATIC),y)
-CFLAGS_busybox += -static
 PKG_CONFIG_FLAGS += --static
 endif
 
 ifeq ($(CONFIG_PIE),y)
+ifeq ($(CONFIG_STATIC),y)
+CFLAGS_busybox += $(ARCH_SPIE)
+else
 CFLAGS_busybox += $(ARCH_PIE)
+endif
 CFLAGS += $(ARCH_FPIE)
+else
+ifeq ($(CONFIG_STATIC),y)
+CFLAGS_busybox += -static
+endif
 endif
 
 ifneq ($(CONFIG_EXTRA_CFLAGS),)
PATCH_EOF

patch -p1 < "$SRCDIR/static-pie.patch"

# ---- Config: static + PIE (now orthogonal after the patch) ----
cp "$SRCDIR/busybox-dot-config" .config
sed -i 's/^# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config
sed -i 's/^# CONFIG_PIE is not set/CONFIG_PIE=y/'    .config
grep -q '^CONFIG_STATIC=y' .config || echo 'CONFIG_STATIC=y' >> .config
grep -q '^CONFIG_PIE=y'    .config || echo 'CONFIG_PIE=y'    >> .config

yes '' | make oldconfig >/dev/null

for c in STATIC PIE; do
  grep -q "^CONFIG_${c}=y" .config || { echo "ERROR: CONFIG_${c} lost" >&2; exit 1; }
done

# ---- Full hardening flags ----
# -static-pie is NOT here; the patched Makefile.flags injects it into
# CFLAGS_busybox, which only affects the final link. Everything else
# applies to both compile and link and is safe on partial links.
HARDEN_CFLAGS="-O2 -fstack-protector-strong -D_FORTIFY_SOURCE=3 \
  -fstack-clash-protection -fcf-protection=full \
  -ftrivial-auto-var-init=zero -fPIE"

HARDEN_LDFLAGS="-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack \
  -Wl,-z,ibt -Wl,-z,shstk"

export CFLAGS="$HARDEN_CFLAGS"
export CXXFLAGS="$HARDEN_CFLAGS"
export LDFLAGS="$HARDEN_LDFLAGS"

make -j"$(nproc)"

# ---- Install: /bin/busybox only, no applet symlinks ----
mkdir -p "$PKG/bin"
cp busybox "$PKG/bin/busybox"
chmod 755 "$PKG/bin/busybox"

# ---- Metadata ----
mkdir -p "$PKG/install"
cat > "$PKG/install/slack-desc" <<'EOF'
busybox: busybox (static PIE multicall binary)
busybox:
busybox: AM·OS installer initrd multicall binary. Static PIE with
busybox: full hardening: stack canary, FORTIFY=3, stack-clash, CET,
busybox: full RELRO, NX stack, and ASLR.
busybox:
busybox: Ships only /bin/busybox. Applet symlinks are created by the
busybox: initrd builder, not by this package.
busybox:
busybox:
EOF

cd "$PKG"
/sbin/makepkg -l y -c n "$OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.txz"

echo
echo "Wrote: $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.txz"