98c14bd008
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Has been cancelled
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
199 lines
8.4 KiB
Bash
199 lines
8.4 KiB
Bash
#!/bin/bash
|
|
# SPDX-Version: 3.0
|
|
# SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; <msw@coresecret.dev>
|
|
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.installer.git
|
|
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
|
|
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <msw@coresecret.dev>
|
|
# SPDX-FileType: SOURCE
|
|
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
|
|
# SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework.
|
|
# SPDX-PackageName: CISS.debian.installer
|
|
# SPDX-Security-Contact: security@coresecret.eu
|
|
|
|
guard_sourcing || return "${ERR_GUARD_SOURCE}"
|
|
|
|
#######################################
|
|
# Build Ultra Hardened dropbear from sources.
|
|
# Globals:
|
|
# TARGET
|
|
# VAR_SETUP_PATH
|
|
# Arguments:
|
|
# None
|
|
# Returns:
|
|
# 0: on success
|
|
# 126: on permission, noexec, or interpreter execution failure
|
|
# 127: on missing build command
|
|
# ERR_PATH_NOT_VALID: on failure
|
|
#######################################
|
|
dropbear_build() {
|
|
### Declare Arrays, HashMaps, and Variables.
|
|
declare var_dropbear_version="2026.91"
|
|
declare var_tar="${VAR_SETUP_PATH}/upgrades/dropbear/dropbear-${var_dropbear_version}.tar.bz2"
|
|
declare var_build_root="/opt/.ciss/build"
|
|
declare var_build_dir="${var_build_root}/dropbear-${var_dropbear_version}"
|
|
declare -r var_logfile="/root/.ciss/cdi/log/4310_dropbear_build.log"
|
|
declare -r var_build_log="${TARGET}${var_logfile}"
|
|
declare -r var_build_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
declare var_command="" var_command_path="" var_configure_interpreter="" var_configure_shebang=""
|
|
declare var_mount_options="" var_mount_target=""
|
|
declare -a ary_required_commands=(bash setsid musl-gcc make nproc)
|
|
|
|
chroot_logger "${var_build_log}"
|
|
|
|
mkdir -p "${var_build_root}"
|
|
chmod 0700 "${var_build_root}"
|
|
|
|
if ! PATH="${var_build_path}" command -v findmnt >/dev/null 2>&1; then
|
|
do_log "error" "file_only" "4310() Required Dropbear build command missing from sterile PATH: 'findmnt'."
|
|
printf 'ERROR: required Dropbear build command missing from sterile PATH: findmnt\n' >> "${var_build_log}"
|
|
return 127
|
|
fi
|
|
|
|
case "${var_build_dir}" in
|
|
/opt/.ciss/build/dropbear-*) ;;
|
|
*)
|
|
do_log "error" "file_only" "4310() Refusing to clean unexpected Dropbear build directory: '${var_build_dir}'."
|
|
printf 'ERROR: refusing to clean unexpected Dropbear build directory: %s\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
;;
|
|
esac
|
|
|
|
rm -rf -- "${var_build_dir}"
|
|
tar xjf "${var_tar}" -C "${var_build_root}" || return "${ERR_PATH_NOT_VALID}"
|
|
|
|
if [[ ! -d "${var_build_dir}" ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear build directory missing: '${var_build_dir}'."
|
|
printf 'ERROR: Dropbear build directory missing: %s\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
fi
|
|
|
|
cp "${VAR_SETUP_PATH}/upgrades/dropbear/localoptions.h" "${var_build_dir}"
|
|
|
|
if ! var_mount_target="$(PATH="${var_build_path}" findmnt -T "${var_build_dir}" -no TARGET)"; then
|
|
do_log "error" "file_only" "4310() Cannot determine mount target for Dropbear build directory: '${var_build_dir}'."
|
|
printf 'ERROR: cannot determine mount target for Dropbear build directory: %s\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
fi
|
|
|
|
if ! var_mount_options="$(PATH="${var_build_path}" findmnt -T "${var_build_dir}" -no OPTIONS)"; then
|
|
do_log "error" "file_only" "4310() Cannot determine mount options for Dropbear build directory: '${var_build_dir}'."
|
|
printf 'ERROR: cannot determine mount options for Dropbear build directory: %s\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
fi
|
|
|
|
if [[ ! -f "${var_build_dir}/configure" ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear configure script missing: '${var_build_dir}/configure'."
|
|
printf 'ERROR: Dropbear configure script missing: %s/configure\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
fi
|
|
|
|
if ! IFS= read -r var_configure_shebang < "${var_build_dir}/configure"; then
|
|
do_log "error" "file_only" "4310() Cannot read Dropbear configure script: '${var_build_dir}/configure'."
|
|
printf 'ERROR: cannot read Dropbear configure script: %s/configure\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return "${ERR_PATH_NOT_VALID}"
|
|
fi
|
|
|
|
if [[ ! -x "${var_build_dir}/configure" ]]; then
|
|
do_log "warn" "file_only" "4310() Dropbear configure script is not executable; correcting trusted source tree mode."
|
|
printf 'WARNING: Dropbear configure script is not executable; correcting trusted source tree mode.\n' >> "${var_build_log}"
|
|
chmod u+x "${var_build_dir}/configure" || return 126
|
|
fi
|
|
|
|
if [[ ! -x "${var_build_dir}/configure" ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear configure script is not executable: '${var_build_dir}/configure'."
|
|
printf 'ERROR: Dropbear configure script is not executable: %s/configure\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return 126
|
|
fi
|
|
|
|
(
|
|
set -Ceuo pipefail
|
|
PATH="${var_build_path}"
|
|
export PATH
|
|
cd "${var_build_dir}"
|
|
printf '4310 Dropbear build preflight diagnostics\n'
|
|
pwd
|
|
id
|
|
for var_command in bash setsid musl-gcc make nproc; do
|
|
if var_command_path="$(command -v "${var_command}")"; then
|
|
printf 'command -v %s: %s\n' "${var_command}" "${var_command_path}"
|
|
else
|
|
printf 'command -v %s: not found\n' "${var_command}"
|
|
fi
|
|
done
|
|
findmnt -T .
|
|
ls -ld .
|
|
ls -l ./configure
|
|
stat ./configure
|
|
head -n 1 ./configure
|
|
) >> "${var_build_log}" 2>&1
|
|
|
|
if [[ ",${var_mount_options}," == *,noexec,* ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear build directory is on a noexec mount: '${var_mount_target}' options '${var_mount_options}'. Use an exec-capable build directory."
|
|
printf 'ERROR: Dropbear build directory is on a noexec mount: %s options %s\n' "${var_mount_target}" "${var_mount_options}" >> "${var_build_log}"
|
|
return 126
|
|
fi
|
|
|
|
if [[ "${var_configure_shebang}" != "#!"* ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear configure script has no interpreter line: '${var_build_dir}/configure'."
|
|
printf 'ERROR: Dropbear configure script has no interpreter line: %s/configure\n' "${var_build_dir}" >> "${var_build_log}"
|
|
return 126
|
|
fi
|
|
|
|
read -r var_configure_interpreter _ <<< "${var_configure_shebang#\#!}"
|
|
if [[ "${var_configure_interpreter}" != /* || ! -x "${var_configure_interpreter}" ]]; then
|
|
do_log "error" "file_only" "4310() Dropbear configure interpreter is not executable: '${var_configure_interpreter}'."
|
|
printf 'ERROR: Dropbear configure interpreter is not executable: %s\n' "${var_configure_interpreter}" >> "${var_build_log}"
|
|
return 126
|
|
fi
|
|
|
|
for var_command in "${ary_required_commands[@]}"; do
|
|
if ! PATH="${var_build_path}" command -v "${var_command}" >/dev/null 2>&1; then
|
|
do_log "error" "file_only" "4310() Required Dropbear build command missing from sterile PATH: '${var_command}'."
|
|
printf 'ERROR: required Dropbear build command missing from sterile PATH: %s\n' "${var_command}" >> "${var_build_log}"
|
|
return 127
|
|
fi
|
|
done
|
|
|
|
### Flag Purpose:
|
|
# -fPIE : Generate position-independent executable code
|
|
# -pie : Link the executable as PIE (so that ASLR works)
|
|
# -static : Fully statically linked against musl
|
|
# -s : Strip unnecessary symbols directly during linking
|
|
# -Wl,-z,relro,-z,now: Enables full RELRO (symbol resolution at program startup)
|
|
|
|
guard_trace on
|
|
|
|
# shellcheck disable=SC2016,SC2312
|
|
PATH="${var_build_path}" setsid bash -c '
|
|
set -Ceuo pipefail
|
|
### Sterile environment for the build-process.
|
|
export -n SHELLOPTS
|
|
unset PATH_SEPARATOR
|
|
PATH_SEPARATOR=":"
|
|
export PATH_SEPARATOR
|
|
PATH="${1}"
|
|
export PATH
|
|
cd "${2}"
|
|
|
|
CC=musl-gcc \
|
|
CFLAGS="-Os -fPIE -Wno-undef -fstack-protector-strong -D_FORTIFY_SOURCE=2" \
|
|
LDFLAGS="-static -pie -s -Wl,-z,relro,-z,now" \
|
|
./configure \
|
|
--enable-static \
|
|
--enable-openpty \
|
|
--disable-pam \
|
|
--disable-zlib
|
|
|
|
# shellcheck disable=2312
|
|
make -j"$(nproc)"
|
|
' bash "${var_build_path}" "${var_build_dir}" >> "${var_build_log}" 2>&1
|
|
|
|
guard_trace off
|
|
|
|
guard_dir; return 0
|
|
}
|
|
### Prevents accidental 'unset -f'.
|
|
# shellcheck disable=SC2034
|
|
readonly -f dropbear_build
|
|
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
|