diff --git a/README.md b/README.md index 8257fc8..52c08dc 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,9 @@ Support for halt,init,poweroff,reboot,runlevel,shutdown,telinit commands --- #### Advanced system features: +##### `ENABLE_SYSTEMDSWAP`=false +Enables [Systemd-swap service](https://github.com/Nefelim4ag/systemd-swap). Usefull if `KERNEL_ZSWAP` is enabled. + ##### `ENABLE_MINBASE`=false Use debootstrap script variant `minbase` which only includes essential packages and apt. This will reduce the disk usage by about 65 MB. @@ -234,6 +237,12 @@ Install and enable the [ARM side libraries for interfacing to Raspberry Pi GPU]( ##### `VIDEOCORESRC_DIR`="" Path to a directory (`userland`) of [ARM side libraries for interfacing to Raspberry Pi GPU](https://github.com/raspberrypi/userland) that will be copied, configured, build and installed inside the chroot. +##### `ENABLE_NEXMON`=false +Install and enable the [Source code for a C-based firmware patching framework for Broadcom/Cypress WiFi chips that enables you to write your own firmware patches, for example, to enable monitor mode with radiotap headers and frame injection](https://github.com/seemoo-lab/nexmon.git). + +##### `NEXMONSRC_DIR`="" +Path to a directory (`nexmon`) of [Source code for ARM side libraries for interfacing to Raspberry Pi GPU](https://github.com/raspberrypi/userland) that will be copied, configured, build and installed inside the chroot. + ##### `ENABLE_IPTABLES`=false Enable iptables IPv4/IPv6 firewall. Simplified ruleset: Allow all outgoing connections. Block all incoming connections except to OpenSSH service. @@ -345,6 +354,23 @@ With this parameter set to true the script expects the existing kernel sources d ##### `RPI_FIRMWARE_DIR`="" The directory (`firmware`) containing a local copy of the firmware from the [RaspberryPi firmware project](https://github.com/raspberrypi/firmware). Default is to download the latest firmware directly from the project. +##### `KERNEL_DEFAULT_GOV`="ONDEMAND" +Set the default cpu governor at kernel compilation. Supported values are: PERFORMANCE POWERSAVE USERSPACE ONDEMAND CONSERVATIVE SCHEDUTIL + +##### `KERNEL_NF`=false +Enable Netfilter modules as kernel modules + +##### `KERNEL_VIRT`=false +Enable Kernel KVM support (/dev/kvm) + +##### `KERNEL_ZSWAP`=false +Enable Kernel Zswap support. Best use on high RAM load and mediocre CPU load usecases + +##### `KERNEL_BPF`=true +Allow attaching eBPF programs to a cgroup using the bpf syscall (CONFIG_BPF_SYSCALL CONFIG_CGROUP_BPF) [systemd compilations about it - File /lib/systemd/system/systemd-journald.server:36 configures an IP firewall (IPAddressDeny=all), but the local system does not support BPF/cgroup based firewalls] + +##### `KERNEL_SECURITY`=false +Enables Apparmor, integrity subsystem, auditing --- #### Reduce disk usage: diff --git a/bootstrap.d/13-kernel.sh b/bootstrap.d/13-kernel.sh index a662049..285bac2 100644 --- a/bootstrap.d/13-kernel.sh +++ b/bootstrap.d/13-kernel.sh @@ -5,6 +5,13 @@ # Load utility functions . ./functions.sh +# Need to use kali kernel src if nexmon is enabled +if [ "$ENABLE_NEXMON" = true ] ; then + echo "WARNING: if ENABLE_NEXMON is used remember to put the CORRECT KERNELSRC IN KERNELSRC_DIR!!!!!1!" + KERNEL_URL="${KALI_KERNEL_URL}" + KERNEL_BRANCH="" +fi + # Fetch and build latest raspberry kernel if [ "$BUILD_KERNEL" = true ] ; then # Setup source directory @@ -86,6 +93,243 @@ if [ "$BUILD_KERNEL" = true ] ; then if [ "$KERNELSRC_CONFIG" = true ] ; then # Load default raspberry kernel configuration make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" "${KERNEL_DEFCONFIG}" + + #Switch to KERNELSRC_DIR so we can use set_kernel_config + cd "${KERNEL_DIR}" || exit + + # enable ZSWAP see https://askubuntu.com/a/472227 or https://wiki.archlinux.org/index.php/zswap + if [ "$KERNEL_ZSWAP" = true ] ; then + set_kernel_config CONFIG_ZPOOL y + set_kernel_config CONFIG_ZSWAP y + set_kernel_config CONFIG_ZBUD y + set_kernel_config CONFIG_Z3FOLD y + set_kernel_config CONFIG_ZSMALLOC y + set_kernel_config CONFIG_PGTABLE_MAPPING y + fi + + # enable basic KVM support; see https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=210546&start=25#p1300453 + if [ "$KERNEL_VIRT" = true ] && { [ "$RPI_MODEL" = 2 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then + set_kernel_config CONFIG_VIRTUALIZATION y + set_kernel_config CONFIG_KVM y + set_kernel_config CONFIG_VHOST_NET m + set_kernel_config CONFIG_VHOST_CROSS_ENDIAN_LEGACY y + fi + + # enable apparmor,integrity audit, + if [ "$KERNEL_SECURITY" = true ] ; then + + # security filesystem, security models and audit + set_kernel_config CONFIG_SECURITYFS y + set_kernel_config CONFIG_SECURITY y + set_kernel_config CONFIG_AUDIT y + + # harden strcpy and memcpy + set_kernel_config CONFIG_HARDENED_USERCOPY=y + set_kernel_config CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR=y + set_kernel_config CONFIG_FORTIFY_SOURCE=y + + # integrity sub-system + set_kernel_config CONFIG_INTEGRITY=y + set_kernel_config CONFIG_INTEGRITY_ASYMMETRIC_KEYS=y + set_kernel_config CONFIG_INTEGRITY_AUDIT=y + set_kernel_config CONFIG_INTEGRITY_SIGNATURE=y + set_kernel_config CONFIG_INTEGRITY_TRUSTED_KEYRING=y + + # This option provides support for retaining authentication tokens and access keys in the kernel. + set_kernel_config CONFIG_KEYS=y + set_kernel_config CONFIG_KEYS_COMPAT=y + + # Apparmor + set_kernel_config CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE 0 + set_kernel_config CONFIG_SECURITY_APPARMOR_HASH_DEFAULT y + set_kernel_config CONFIG_DEFAULT_SECURITY_APPARMOR y + set_kernel_config CONFIG_SECURITY_APPARMOR y + set_kernel_config CONFIG_SECURITY_APPARMOR_HASH y + set_kernel_config CONFIG_DEFAULT_SECURITY "apparmor" + + # restrictions on unprivileged users reading the kernel + set_kernel_config CONFIG_SECURITY_DMESG_RESTRICT=y + + # network security hooks + set_kernel_config CONFIG_SECURITY_NETWORK y + set_kernel_config CONFIG_SECURITY_NETWORK_XFRM=y + set_kernel_config CONFIG_SECURITY_PATH=y + set_kernel_config CONFIG_SECURITY_YAMA=y + + # New Options + if [ "$KERNEL_NF" = true ] + set_kernel_config CONFIG_IP_NF_SECURITY m + set_kernel_config CONFIG_NETLABEL m + set_kernel_config CONFIG_IP6_NF_SECURITY m + fi + set_kernel_config CONFIG_SECURITY_SELINUX n + set_kernel_config CONFIG_SECURITY_SMACK n + set_kernel_config CONFIG_SECURITY_TOMOYO n + set_kernel_config CONFIG_SECURITY_APPARMOR_DEBUG n + set_kernel_config CONFIG_SECURITY_LOADPIN n + set_kernel_config CONFIG_HARDENED_USERCOPY_PAGESPAN n + set_kernel_config CONFIG_IMA n + set_kernel_config CONFIG_EVM n + set_kernel_config CONFIG_FANOTIFY_ACCESS_PERMISSIONS y + set_kernel_config CONFIG_NFSD_V4_SECURITY_LABEL y + set_kernel_config CONFIG_PKCS7_MESSAGE_PARSER y + set_kernel_config CONFIG_SYSTEM_TRUSTED_KEYRING y + set_kernel_config CONFIG_SYSTEM_TRUSTED_KEYS y + set_kernel_config CONFIG_SYSTEM_EXTRA_CERTIFICATE y + set_kernel_config CONFIG_SECONDARY_TRUSTED_KEYRING y + set_kernel_config CONFIG_IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY n + fi + + # Netfilter kernel support See https://github.com/raspberrypi/linux/issues/2177#issuecomment-354647406 + if [ "$KERNEL_NF" = true ] ; then + set_kernel_config CONFIG_IP_NF_TARGET_SYNPROXY m + set_kernel_config CONFIG_NETFILTER_XT_TARGET_AUDIT m + set_kernel_config CONFIG_NETFILTER_XT_MATCH_CGROUP m + set_kernel_config CONFIG_NETFILTER_XT_MATCH_IPCOMP m + set_kernel_config CONFIG_NETFILTER_XT_MATCH_SOCKET m + set_kernel_config CONFIG_NFT_FIB_INET m + set_kernel_config CONFIG_NFT_FIB_IPV4 m + set_kernel_config CONFIG_NFT_FIB_IPV6 m + set_kernel_config CONFIG_NFT_FIB_NETDEV m + set_kernel_config CONFIG_NFT_OBJREF m + set_kernel_config CONFIG_NFT_RT m + set_kernel_config CONFIG_NFT_SET_BITMAP m + set_kernel_config CONFIG_NF_CONNTRACK_TIMEOUT y + set_kernel_config CONFIG_NF_LOG_ARP m + set_kernel_config CONFIG_NF_SOCKET_IPV4 m + set_kernel_config CONFIG_NF_SOCKET_IPV6 m + set_kernel_config CONFIG_BRIDGE_EBT_BROUTE m + set_kernel_config CONFIG_BRIDGE_EBT_T_FILTER m + set_kernel_config CONFIG_BRIDGE_NF_EBTABLES m + set_kernel_config CONFIG_IP6_NF_IPTABLES m + set_kernel_config CONFIG_IP6_NF_MATCH_AH m + set_kernel_config CONFIG_IP6_NF_MATCH_EUI64 m + set_kernel_config CONFIG_IP6_NF_NAT m + set_kernel_config CONFIG_IP6_NF_TARGET_MASQUERADE m + set_kernel_config CONFIG_IP6_NF_TARGET_NPT m + set_kernel_config CONFIG_IP_NF_SECURITY m + set_kernel_config CONFIG_IP_SET_BITMAP_IPMAC m + set_kernel_config CONFIG_IP_SET_BITMAP_PORT m + set_kernel_config CONFIG_IP_SET_HASH_IP m + set_kernel_config CONFIG_IP_SET_HASH_IPMARK m + set_kernel_config CONFIG_IP_SET_HASH_IPPORT m + set_kernel_config CONFIG_IP_SET_HASH_IPPORTIP m + set_kernel_config CONFIG_IP_SET_HASH_IPPORTNET m + set_kernel_config CONFIG_IP_SET_HASH_MAC m + set_kernel_config CONFIG_IP_SET_HASH_NET m + set_kernel_config CONFIG_IP_SET_HASH_NETIFACE m + set_kernel_config CONFIG_IP_SET_HASH_NETNET m + set_kernel_config CONFIG_IP_SET_HASH_NETPORT m + set_kernel_config CONFIG_IP_SET_HASH_NETPORTNET m + set_kernel_config CONFIG_IP_SET_LIST_SET m + set_kernel_config CONFIG_NETFILTER_XTABLES m + set_kernel_config CONFIG_NETFILTER_XTABLES m + set_kernel_config CONFIG_NFT_BRIDGE_META m + set_kernel_config CONFIG_NFT_BRIDGE_REJECT m + set_kernel_config CONFIG_NFT_CHAIN_NAT_IPV4 m + set_kernel_config CONFIG_NFT_CHAIN_NAT_IPV6 m + set_kernel_config CONFIG_NFT_CHAIN_ROUTE_IPV4 m + set_kernel_config CONFIG_NFT_CHAIN_ROUTE_IPV6 m + set_kernel_config CONFIG_NFT_COMPAT m + set_kernel_config CONFIG_NFT_COUNTER m + set_kernel_config CONFIG_NFT_CT m + set_kernel_config CONFIG_NFT_DUP_IPV4 m + set_kernel_config CONFIG_NFT_DUP_IPV6 m + set_kernel_config CONFIG_NFT_DUP_NETDEV m + set_kernel_config CONFIG_NFT_EXTHDR m + set_kernel_config CONFIG_NFT_FWD_NETDEV m + set_kernel_config CONFIG_NFT_HASH m + set_kernel_config CONFIG_NFT_LIMIT m + set_kernel_config CONFIG_NFT_LOG m + set_kernel_config CONFIG_NFT_MASQ m + set_kernel_config CONFIG_NFT_MASQ_IPV4 m + set_kernel_config CONFIG_NFT_MASQ_IPV6 m + set_kernel_config CONFIG_NFT_META m + set_kernel_config CONFIG_NFT_NAT m + set_kernel_config CONFIG_NFT_NUMGEN m + set_kernel_config CONFIG_NFT_QUEUE m + set_kernel_config CONFIG_NFT_QUOTA m + set_kernel_config CONFIG_NFT_REDIR m + set_kernel_config CONFIG_NFT_REDIR_IPV4 m + set_kernel_config CONFIG_NFT_REDIR_IPV6 m + set_kernel_config CONFIG_NFT_REJECT m + set_kernel_config CONFIG_NFT_REJECT_INET m + set_kernel_config CONFIG_NFT_REJECT_IPV4 m + set_kernel_config CONFIG_NFT_REJECT_IPV6 m + set_kernel_config CONFIG_NFT_SET_HASH m + set_kernel_config CONFIG_NFT_SET_RBTREE m + set_kernel_config CONFIG_NF_CONNTRACK_IPV4 m + set_kernel_config CONFIG_NF_CONNTRACK_IPV6 m + set_kernel_config CONFIG_NF_DEFRAG_IPV4 m + set_kernel_config CONFIG_NF_DEFRAG_IPV6 m + set_kernel_config CONFIG_NF_DUP_IPV4 m + set_kernel_config CONFIG_NF_DUP_IPV6 m + set_kernel_config CONFIG_NF_DUP_NETDEV m + set_kernel_config CONFIG_NF_LOG_BRIDGE m + set_kernel_config CONFIG_NF_LOG_IPV4 m + set_kernel_config CONFIG_NF_LOG_IPV6 m + set_kernel_config CONFIG_NF_NAT_IPV4 m + set_kernel_config CONFIG_NF_NAT_IPV6 m + set_kernel_config CONFIG_NF_NAT_MASQUERADE_IPV4 m + set_kernel_config CONFIG_NF_NAT_MASQUERADE_IPV6 m + set_kernel_config CONFIG_NF_NAT_PPTP m + set_kernel_config CONFIG_NF_NAT_PROTO_GRE m + set_kernel_config CONFIG_NF_NAT_REDIRECT m + set_kernel_config CONFIG_NF_NAT_SIP m + set_kernel_config CONFIG_NF_NAT_SNMP_BASIC m + set_kernel_config CONFIG_NF_NAT_TFTP m + set_kernel_config CONFIG_NF_REJECT_IPV4 m + set_kernel_config CONFIG_NF_REJECT_IPV6 m + set_kernel_config CONFIG_NF_TABLES m + set_kernel_config CONFIG_NF_TABLES_ARP m + set_kernel_config CONFIG_NF_TABLES_BRIDGE m + set_kernel_config CONFIG_NF_TABLES_INET m + set_kernel_config CONFIG_NF_TABLES_IPV4 m + set_kernel_config CONFIG_NF_TABLES_IPV6 m + set_kernel_config CONFIG_NF_TABLES_NETDEV m + fi + + # Enables BPF syscall for systemd-journald see https://github.com/torvalds/linux/blob/master/init/Kconfig#L848 or https://groups.google.com/forum/#!topic/linux.gentoo.user/_2aSc_ztGpA + if [ "$KERNEL_BPF" = true ] ; then + set_kernel_config CONFIG_BPF_SYSCALL y + set_kernel_config CONFIG_BPF_EVENTS y + set_kernel_config CONFIG_BPF_STREAM_PARSER y + set_kernel_config CONFIG_CGROUP_BPF y + fi + + # KERNEL_DEFAULT_GOV was set by user + if [ "$KERNEL_DEFAULT_GOV" != powersave ] && [ -n "$KERNEL_DEFAULT_GOV" ]; then + + case "$KERNEL_DEFAULT_GOV" in + performance) + set_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE y + ;; + userspace) + set_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE y + ;; + ondemand) + set_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND y + ;; + conservative) + set_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE y + ;; + shedutil) + set_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL y + ;; + *) + echo "error: unsupported default cpu governor" + exit 1 + ;; + esac + + # unset previous default governor + unset_kernel_config CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE + fi + + + + #Revert to previous directory + cd "${WORKDIR}" || exit # Set kernel configuration parameters to enable qemu emulation if [ "$ENABLE_QEMU" = true ] ; then @@ -126,6 +370,7 @@ if [ "$BUILD_KERNEL" = true ] ; then if [ "$KERNEL_MENUCONFIG" = true ] ; then make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" menuconfig fi + # end if "$KERNELSRC_CONFIG" = true fi # Use ccache to cross compile the kernel @@ -142,6 +387,7 @@ if [ "$BUILD_KERNEL" = true ] ; then if grep -q "CONFIG_MODULES=y" "${KERNEL_DIR}/.config" ; then make -C "${KERNEL_DIR}" -j"${KERNEL_THREADS}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" modules fi + # end if "$KERNELSRC_PREBUILT" = false fi # Check if kernel compilation was successful @@ -237,19 +483,79 @@ if [ "$BUILD_KERNEL" = true ] ; then fi else # BUILD_KERNEL=false - # Kernel installation - chroot_exec apt-get -qq -y --no-install-recommends install linux-image-"${COLLABORA_KERNEL}" raspberrypi-bootloader-nokernel + if [ "$SET_ARCH" = 64 ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then + + # Use Sakakis modified kernel if ZSWAP is active + if [ "$KERNEL_ZSWAP" = true ] || [ "$KERNEL_VIRT" = true ] || [ "$KERNEL_NF" = true ] || [ "$KERNEL_BPF" = true ] ; then + RPI3_64_KERNEL_URL="${RPI3_64_BIS_KERNEL_URL}" + fi + + # Create temporary directory for dl + temp_dir=$(as_nobody mktemp -d) + + # Fetch kernel dl + as_nobody wget -O "${temp_dir}"/kernel.tar.xz -c "$RPI3_64_KERNEL_URL" + + #extract download + tar -xJf "${temp_dir}"/kernel.tar.xz -C "${temp_dir}" - # Install flash-kernel last so it doesn't try (and fail) to detect the platform in the chroot - chroot_exec apt-get -qq -y install flash-kernel + #move extracted kernel to /boot/firmware + mkdir "${R}/boot/firmware" + cp "${temp_dir}"/boot/* "${R}"/boot/firmware/ + cp -r "${temp_dir}"/lib/* "${R}"/lib/ + + # Remove temporary directory for kernel sources + rm -fr "${temp_dir}" + + # Set permissions of the kernel sources + chown -R root:root "${R}/boot/firmware" + chown -R root:root "${R}/lib/modules" + fi + + # Install Kernel from hypriot comptabile with all Raspberry PI + if [ "$SET_ARCH" = 32 ] ; then + # Create temporary directory for dl + temp_dir=$(as_nobody mktemp -d) + + # Fetch kernel + as_nobody wget -O "${temp_dir}"/kernel.deb -c "$RPI_32_KERNEL_URL" + + # Copy downloaded U-Boot sources + mv "${temp_dir}"/kernel.deb "${R}"/tmp/kernel.deb + + # Set permissions + chown -R root:root "${R}"/tmp/kernel.deb + + # Install kernel + chroot_exec dpkg -i /tmp/kernel.deb + + # move /boot to /boot/firmware to fit script env. + #mkdir "${BOOT_DIR}" + mkdir "${temp_dir}"/firmware + mv "${R}"/boot/* "${temp_dir}"/firmware/ + mv "${temp_dir}"/firmware "${R}"/boot/ + + #same for kernel headers + if [ "$KERNEL_HEADERS" = true ] ; then + # Fetch kernel header + as_nobody wget -O "${temp_dir}"/kernel-header.deb -c "$RPI_32_KERNELHEADER_URL" + mv "${temp_dir}"/kernel-header.deb "${R}"/tmp/kernel-header.deb + chown -R root:root "${R}"/tmp/kernel-header.deb + # Install kernel header + chroot_exec dpkg -i /tmp/kernel-header.deb + rm -f "${R}"/tmp/kernel-header.deb + fi + + # Remove temporary directory and files + rm -fr "${temp_dir}" + rm -f "${R}"/tmp/kernel.deb + fi # Check if kernel installation was successful - VMLINUZ="$(ls -1 "${R}"/boot/vmlinuz-* | sort | tail -n 1)" - if [ -z "$VMLINUZ" ] ; then - echo "error: kernel installation failed! (/boot/vmlinuz-* not found)" + KERNEL="$(ls -1 "${R}"/boot/firmware/kernel* | sort | tail -n 1)" + if [ -z "$KERNEL" ] ; then + echo "error: kernel installation failed! (/boot/kernel* not found)" cleanup exit 1 fi - # Copy vmlinuz kernel to the boot directory - install_readonly "${VMLINUZ}" "${BOOT_DIR}/${KERNEL_IMAGE}" fi diff --git a/bootstrap.d/15-rpi-config.sh b/bootstrap.d/15-rpi-config.sh index eda69bd..b3fb928 100644 --- a/bootstrap.d/15-rpi-config.sh +++ b/bootstrap.d/15-rpi-config.sh @@ -5,39 +5,37 @@ # Load utility functions . ./functions.sh -if [ "$BUILD_KERNEL" = true ] ; then - if [ -n "$RPI_FIRMWARE_DIR" ] && [ -d "$RPI_FIRMWARE_DIR" ] ; then - # Install boot binaries from local directory - cp "${RPI_FIRMWARE_DIR}"/boot/bootcode.bin "${BOOT_DIR}"/bootcode.bin - cp "${RPI_FIRMWARE_DIR}"/boot/fixup.dat "${BOOT_DIR}"/fixup.dat - cp "${RPI_FIRMWARE_DIR}"/boot/fixup_cd.dat "${BOOT_DIR}"/fixup_cd.dat - cp "${RPI_FIRMWARE_DIR}"/boot/fixup_x.dat "${BOOT_DIR}"/fixup_x.dat - cp "${RPI_FIRMWARE_DIR}"/boot/start.elf "${BOOT_DIR}"/start.elf - cp "${RPI_FIRMWARE_DIR}"/boot/start_cd.elf "${BOOT_DIR}"/start_cd.elf - cp "${RPI_FIRMWARE_DIR}"/boot/start_x.elf "${BOOT_DIR}"/start_x.elf - else - # Create temporary directory for boot binaries - temp_dir=$(as_nobody mktemp -d) - - # Install latest boot binaries from raspberry/firmware github - as_nobody wget -q -O "${temp_dir}/bootcode.bin" "${FIRMWARE_URL}/bootcode.bin" - as_nobody wget -q -O "${temp_dir}/fixup.dat" "${FIRMWARE_URL}/fixup.dat" - as_nobody wget -q -O "${temp_dir}/fixup_cd.dat" "${FIRMWARE_URL}/fixup_cd.dat" - as_nobody wget -q -O "${temp_dir}/fixup_x.dat" "${FIRMWARE_URL}/fixup_x.dat" - as_nobody wget -q -O "${temp_dir}/start.elf" "${FIRMWARE_URL}/start.elf" - as_nobody wget -q -O "${temp_dir}/start_cd.elf" "${FIRMWARE_URL}/start_cd.elf" - as_nobody wget -q -O "${temp_dir}/start_x.elf" "${FIRMWARE_URL}/start_x.elf" - - # Move downloaded boot binaries - mv "${temp_dir}/"* "${BOOT_DIR}/" - - # Remove temporary directory for boot binaries - rm -fr "${temp_dir}" - - # Set permissions of the boot binaries - chown -R root:root "${BOOT_DIR}" - chmod -R 600 "${BOOT_DIR}" - fi +if [ -n "$RPI_FIRMWARE_DIR" ] && [ -d "$RPI_FIRMWARE_DIR" ] ; then + # Install boot binaries from local directory + cp "${RPI_FIRMWARE_DIR}"/boot/bootcode.bin "${BOOT_DIR}"/bootcode.bin + cp "${RPI_FIRMWARE_DIR}"/boot/fixup.dat "${BOOT_DIR}"/fixup.dat + cp "${RPI_FIRMWARE_DIR}"/boot/fixup_cd.dat "${BOOT_DIR}"/fixup_cd.dat + cp "${RPI_FIRMWARE_DIR}"/boot/fixup_x.dat "${BOOT_DIR}"/fixup_x.dat + cp "${RPI_FIRMWARE_DIR}"/boot/start.elf "${BOOT_DIR}"/start.elf + cp "${RPI_FIRMWARE_DIR}"/boot/start_cd.elf "${BOOT_DIR}"/start_cd.elf + cp "${RPI_FIRMWARE_DIR}"/boot/start_x.elf "${BOOT_DIR}"/start_x.elf +else + # Create temporary directory for boot binaries + temp_dir=$(as_nobody mktemp -d) + + # Install latest boot binaries from raspberry/firmware github + as_nobody wget -q -O "${temp_dir}/bootcode.bin" "${FIRMWARE_URL}/bootcode.bin" + as_nobody wget -q -O "${temp_dir}/fixup.dat" "${FIRMWARE_URL}/fixup.dat" + as_nobody wget -q -O "${temp_dir}/fixup_cd.dat" "${FIRMWARE_URL}/fixup_cd.dat" + as_nobody wget -q -O "${temp_dir}/fixup_x.dat" "${FIRMWARE_URL}/fixup_x.dat" + as_nobody wget -q -O "${temp_dir}/start.elf" "${FIRMWARE_URL}/start.elf" + as_nobody wget -q -O "${temp_dir}/start_cd.elf" "${FIRMWARE_URL}/start_cd.elf" + as_nobody wget -q -O "${temp_dir}/start_x.elf" "${FIRMWARE_URL}/start_x.elf" + + # Move downloaded boot binaries + mv "${temp_dir}/"* "${BOOT_DIR}/" + + # Remove temporary directory for boot binaries + rm -fr "${temp_dir}" + + # Set permissions of the boot binaries + chown -R root:root "${BOOT_DIR}" + chmod -R 600 "${BOOT_DIR}" fi # Setup firmware boot cmdline @@ -56,18 +54,36 @@ if [ "$ENABLE_CRYPTFS" = true ] ; then fi fi -#locks cpu at max frequency -if [ "$ENABLE_TURBO" = true ] ; then - echo "force_turbo=1" >> "${BOOT_DIR}/config.txt" -fi - +# Enable Kernel messages on standard output if [ "$ENABLE_PRINTK" = true ] ; then install_readonly files/sysctl.d/83-rpi-printk.conf "${ETC_DIR}/sysctl.d/83-rpi-printk.conf" fi -# Install udev rule for serial alias +# Install udev rule for serial alias - serial0 = console serial1=bluetooth install_readonly files/etc/99-com.rules "${LIB_DIR}/udev/rules.d/99-com.rules" +# Remove IPv6 networking support +if [ "$ENABLE_IPV6" = false ] ; then + CMDLINE="${CMDLINE} ipv6.disable=1" +fi + +# Automatically assign predictable network interface names +if [ "$ENABLE_IFNAMES" = false ] ; then + CMDLINE="${CMDLINE} net.ifnames=0" +else + CMDLINE="${CMDLINE} net.ifnames=1" +fi + +# Install firmware config +install_readonly files/boot/config.txt "${BOOT_DIR}/config.txt" + +# Locks CPU frequency at maximum +if [ "$ENABLE_TURBO" = true ] ; then + echo "force_turbo=1" >> "${BOOT_DIR}/config.txt" + # helps to avoid sdcard corruption when force_turbo is enabled. + echo "boot_delay=1" >> "${BOOT_DIR}/config.txt" +fi + if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then # RPI0,3,3P Use default ttyS0 (mini-UART)as serial interface @@ -152,24 +168,49 @@ else chroot_exec systemctl disable serial-getty\@"$SET_SERIAL".service fi -# Remove IPv6 networking support -if [ "$ENABLE_IPV6" = false ] ; then - CMDLINE="${CMDLINE} ipv6.disable=1" -fi +if [ "$ENABLE_SYSTEMDSWAP" = true ] ; then + # Create temporary directory for systemd-swap sources + temp_dir=$(as_nobody mktemp -d) -# Automatically assign predictable network interface names -if [ "$ENABLE_IFNAMES" = false ] ; then - CMDLINE="${CMDLINE} net.ifnames=0" + # Fetch systemd-swap sources + as_nobody git -C "${temp_dir}" clone "${SYSTEMDSWAP_URL}" + + # Copy downloaded systemd-swap sources + mv "${temp_dir}/systemd-swap" "${R}/tmp/" + + # Set permissions of the systemd-swap sources + chown -R root:root "${R}/tmp/systemd-swap" + + # Remove temporary directory for systemd-swap sources + rm -fr "${temp_dir}" + + # Change into downloaded src dir + cd "${R}/tmp/systemd-swap" || exit + + # Build package + . ./package.sh debian + + # Install package + chroot_exec dpkg -i /tmp/systemd-swap/systemd-swap-*any.deb + + # Enable service + chroot_exec systemctl enable systemd-swap + + # Change back into script root dir + cd "${WORKDIR}" || exit else - CMDLINE="${CMDLINE} net.ifnames=1" + # Enable ZSWAP in cmdline if systemd-swap is not used + if [ "$KERNEL_ZSWAP" = true ] ; then + CMDLINE="${CMDLINE} zswap.enabled=1 zswap.max_pool_percent=25 zswap.compressor=lz4" + fi fi + if [ "$KERNEL_SECURITY" = true ] ; then + CMDLINE="${CMDLINE} apparmor=1 security=apparmor" + fi # Install firmware boot cmdline echo "${CMDLINE}" > "${BOOT_DIR}/cmdline.txt" -# Install firmware config -install_readonly files/boot/config.txt "${BOOT_DIR}/config.txt" - # Setup minimal GPU memory allocation size: 16MB (no X) if [ "$ENABLE_MINGPU" = true ] ; then echo "gpu_mem=16" >> "${BOOT_DIR}/config.txt" diff --git a/bootstrap.d/21-firewall.sh b/bootstrap.d/21-firewall.sh index e6033c4..164033c 100644 --- a/bootstrap.d/21-firewall.sh +++ b/bootstrap.d/21-firewall.sh @@ -9,9 +9,10 @@ if [ "$ENABLE_IPTABLES" = true ] ; then # Create iptables configuration directory mkdir -p "${ETC_DIR}/iptables" - # make sure iptables-legacy is the used alternatives - #iptables-save and -restore are slaves of iptables and thus are set accordingly - chroot_exec update-alternatives --verbose --set iptables /usr/sbin/iptables-legacy + if [ "$KERNEL_NF" = false ] ; then + #iptables-save and -restore are slaves of iptables and thus are set accordingly + chroot_exec update-alternatives --verbose --set iptables /usr/sbin/iptables-legacy + fi # Install iptables systemd service install_readonly files/iptables/iptables.service "${ETC_DIR}/systemd/system/iptables.service" @@ -27,6 +28,10 @@ if [ "$ENABLE_IPTABLES" = true ] ; then chroot_exec systemctl enable iptables.service if [ "$ENABLE_IPV6" = true ] ; then + if [ "$KERNEL_NF" = false ] ; then + #iptables-save and -restore are slaves of iptables and thus are set accordingly + chroot_exec update-alternatives --verbose --set ip6tables /usr/sbin/ip6tables-legacy + fi # Install ip6tables systemd service install_readonly files/iptables/ip6tables.service "${ETC_DIR}/systemd/system/ip6tables.service" diff --git a/bootstrap.d/44-nexmon_monitor_patch.sh b/bootstrap.d/44-nexmon_monitor_patch.sh new file mode 100644 index 0000000..5a260a2 --- /dev/null +++ b/bootstrap.d/44-nexmon_monitor_patch.sh @@ -0,0 +1,97 @@ +#!/bin/sh +# +# Build and Setup nexmon with monitor mode patch +# + +# Load utility functions +. ./functions.sh + +if [ "$ENABLE_NEXMON" = true ] && [ "$ENABLE_WIRELESS" = true ]; then + # Copy existing nexmon sources into chroot directory + if [ -n "$NEXMONSRC_DIR" ] && [ -d "$NEXMONSRC_DIR" ] ; then + # Copy local U-Boot sources + cp -r "${NEXMONSRC_DIR}" "${R}/tmp" + else + # Create temporary directory for nexmon sources + temp_dir=$(as_nobody mktemp -d) + + # Fetch nexmon sources + as_nobody git -C "${temp_dir}" clone "${NEXMON_URL}" + + # Copy downloaded nexmon sources + mv "${temp_dir}/nexmon" "${R}"/tmp/ + + # Set permissions of the nexmon sources + chown -R root:root "${R}"/tmp/nexmon + + # Remove temporary directory for nexmon sources + rm -fr "${temp_dir}" + fi + + # Set script Root + export NEXMON_ROOT="${R}"/tmp/nexmon + + # Build nexmon firmware outside the build system, if we can. + cd "${NEXMON_ROOT}" || exit + + # Make ancient isl build + cd buildtools/isl-0.10 || exit + ./configure + make + cd ../.. || exit + + # Disable statistics + touch DISABLE_STATISTICS + + # Setup Enviroment: see https://github.com/NoobieDog/nexmon/blob/master/setup_env.sh + export KERNEL="${KERNEL_IMAGE}" + export ARCH=arm + export SUBARCH=arm + export CC="${NEXMON_ROOT}"/buildtools/gcc-arm-none-eabi-5_4-2016q2-linux-x86/bin/arm-none-eabi- + export CC="${CC}"gcc + export CCPLUGIN="${NEXMON_ROOT}"/buildtools/gcc-nexmon-plugin/nexmon.so + export ZLIBFLATE="zlib-flate -compress" + export Q=@ + export NEXMON_SETUP_ENV=1 + export HOSTUNAME=$(uname -s) + export PLATFORMUNAME=$(uname -m) + + # Make nexmon + make + + # build patches + if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] ; then + cd "${NEXMON_ROOT}"/patches/bcm43430a1/7_45_41_46/nexmon || exit + sed -i -e 's/all:.*/all: $(RAM_FILE)/g' ${NEXMON_ROOT}/patches/bcm43430a1/7_45_41_46/nexmon/Makefile + make clean + + # We do this so we don't have to install the ancient isl version into /usr/local/lib on systems. + LD_LIBRARY_PATH="${NEXMON_ROOT}"/buildtools/isl-0.10/.libs make ARCH="${KERNEL_ARCH}" CC="${NEXMON_ROOT}"/buildtools/gcc-arm-none-eabi-5_4-2016q2-linux-x86/bin/arm-none-eabi- + + # copy RPi0W & RPi3 firmware + mv "${WLAN_FIRMWARE_DIR}"/brcmfmac43430-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43430-sdio.org.bin + cp "${NEXMON_ROOT}"/patches/bcm43430a1/7_45_41_46/nexmon/brcmfmac43430-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43430-sdio.nexmon.bin + cp -f "${NEXMON_ROOT}"/patches/bcm43430a1/7_45_41_46/nexmon/brcmfmac43430-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43430-sdio.bin + fi + + if [ "$RPI_MODEL" = 3P ] ; then + cd "${NEXMON_ROOT}"/patches/bcm43455c0/7_45_154/nexmon || exit + sed -i -e 's/all:.*/all: $(RAM_FILE)/g' ${NEXMON_ROOT}/patches/bcm43455c0/7_45_154/nexmon/Makefile + make clean + + # We do this so we don't have to install the ancient isl version into /usr/local/lib on systems. + LD_LIBRARY_PATH=${NEXMON_ROOT}/buildtools/isl-0.10/.libs make ARCH="${KERNEL_ARCH}" CC="${NEXMON_ROOT}"/buildtools/gcc-arm-none-eabi-5_4-2016q2-linux-x86/bin/arm-none-eabi- + + # RPi3B+ firmware + mv "${WLAN_FIRMWARE_DIR}"/brcmfmac43455-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43455-sdio.org.bin + cp "${NEXMON_ROOT}"/patches/bcm43455c0/7_45_154/nexmon/brcmfmac43455-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43455-sdio.nexmon.bin + cp -f "${NEXMON_ROOT}"/patches/bcm43455c0/7_45_154/nexmon/brcmfmac43455-sdio.bin "${WLAN_FIRMWARE_DIR}"/brcmfmac43455-sdio.bin + fi + +#Revert to previous directory +cd "${WORKDIR}" || exit + +# Remove nexmon sources +rm -fr "${NEXMON_ROOT}" + +fi diff --git a/rpi23-gen-image.sh b/rpi23-gen-image.sh index 575252a..07dd320 100755 --- a/rpi23-gen-image.sh +++ b/rpi23-gen-image.sh @@ -57,6 +57,20 @@ FBTURBO_URL=${FBTURBO_URL:=https://github.com/ssvb/xf86-video-fbturbo.git} UBOOT_URL=${UBOOT_URL:=https://git.denx.de/u-boot.git} VIDEOCORE_URL=${VIDEOCORE_URL:=https://github.com/raspberrypi/userland} BLUETOOTH_URL=${BLUETOOTH_URL:=https://github.com/RPi-Distro/pi-bluetooth.git} +NEXMON_URL=${NEXMON_URL:=https://github.com/seemoo-lab/nexmon.git} +SYSTEMDSWAP_URL=${SYSTEMDSWAP_URL:=https://github.com/Nefelim4ag/systemd-swap.git} + +# Kernel deb packages for 32bit kernel +RPI_32_KERNEL_URL=${RPI_32_KERNEL_URL:=https://github.com/hypriot/rpi-kernel/releases/download/v4.14.34/raspberrypi-kernel_20180422-141901_armhf.deb} +RPI_32_KERNELHEADER_URL=${RPI_32_KERNELHEADER_URL:=https://github.com/hypriot/rpi-kernel/releases/download/v4.14.34/raspberrypi-kernel-headers_20180422-141901_armhf.deb} +# Kernel has KVM and zswap enabled - use if KERNEL_* parameters and precompiled kernel are used +RPI3_64_BIS_KERNEL_URL=${RPI3_64_BIS_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel-bis/releases/download/4.14.80.20181113/bcmrpi3-kernel-bis-4.14.80.20181113.tar.xz} +# Default precompiled 64bit kernel +RPI3_64_DEF_KERNEL_URL=${RPI3_64_DEF_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel/releases/download/4.14.80.20181113/bcmrpi3-kernel-4.14.80.20181113.tar.xz} +# Generic +RPI3_64_KERNEL_URL=${RPI3_64_KERNEL_URL:=$RPI3_64_DEF_KERNEL_URL} +# Kali kernel src - used if ENABLE_NEXMON=true (they patch the wlan kernel modul) +KALI_KERNEL_URL=${KALI_KERNEL_URL:=https://github.com/Re4son/re4son-raspberrypi-linux.git} # Build directories WORKDIR=$(pwd) @@ -138,14 +152,17 @@ SSH_ROOT_PUB_KEY=${SSH_ROOT_PUB_KEY:=""} SSH_USER_PUB_KEY=${SSH_USER_PUB_KEY:=""} # Advanced settings +ENABLE_SYSTEMDSWAP=${ENABLE_SYSTEMDSWAP:=false} ENABLE_MINBASE=${ENABLE_MINBASE:=false} ENABLE_REDUCE=${ENABLE_REDUCE:=false} ENABLE_UBOOT=${ENABLE_UBOOT:=false} UBOOTSRC_DIR=${UBOOTSRC_DIR:=""} ENABLE_FBTURBO=${ENABLE_FBTURBO:=false} ENABLE_VIDEOCORE=${ENABLE_VIDEOCORE:=false} +ENABLE_NEXMON=${ENABLE_NEXMON:=false} VIDEOCORESRC_DIR=${VIDEOCORESRC_DIR:=""} FBTURBOSRC_DIR=${FBTURBOSRC_DIR:=""} +NEXMONSRC_DIR=${NEXMONSRC_DIR:=""} ENABLE_HARDNET=${ENABLE_HARDNET:=false} ENABLE_IPTABLES=${ENABLE_IPTABLES:=false} ENABLE_SPLITFS=${ENABLE_SPLITFS:=false} @@ -162,6 +179,12 @@ KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false} KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true} KERNEL_OLDDEFCONFIG=${KERNEL_OLDDEFCONFIG:=false} KERNEL_CCACHE=${KERNEL_CCACHE:=false} +KERNEL_ZSWAP=${KERNEL_ZSWAP:=false} +KERNEL_VIRT=${KERNEL_VIRT:=false} +KERNEL_BPF=${KERNEL_BPF:=false} +KERNEL_DEFAULT_GOV=${KERNEL_DEFAULT_GOV:=powersave} +KERNEL_SECURITY=${KERNEL_SECURITY:=false} +KERNEL_NF=${KERNEL_NF:=false} # Kernel compilation from source directory settings KERNELSRC_DIR=${KERNELSRC_DIR:=""} @@ -258,7 +281,7 @@ if [ -n "$SET_ARCH" ] ; then CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabihf-} fi fi -#SET_ARCH not set +# SET_ARCH not set else echo "error: Please set '32' or '64' as value for SET_ARCH" exit 1 @@ -298,9 +321,12 @@ esac # Raspberry PI 0,3,3P with Bluetooth and Wifi onboard if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then # Include bluetooth packages on supported boards - if [ "$ENABLE_BLUETOOTH" = true ] && [ "$ENABLE_CONSOLE" = false ]; then + if [ "$ENABLE_BLUETOOTH" = true ] ; then APT_INCLUDES="${APT_INCLUDES},bluetooth,bluez" fi + if [ "$ENABLE_WIRELESS" = true ] ; then + APT_INCLUDES="${APT_INCLUDES},wireless-tools,crda,wireless-regdb" + fi else # Raspberry PI 1,1P,2 without Wifi and bluetooth onboard # Check if the internal wireless interface is not supported by the RPi model if [ "$ENABLE_WIRELESS" = true ] || [ "$ENABLE_BLUETOOTH" = true ]; then @@ -309,6 +335,11 @@ else # Raspberry PI 1,1P,2 without Wifi and bluetooth onboard fi fi +if [ "$BUILD_KERNEL" = false ] && [ "$ENABLE_NEXMON" = true ]; then + echo "error: You have to compile kernel sources, if you want to enable nexmon" + exit 1 +fi + # Prepare date string for default image file name DATE="$(date +%Y-%m-%d)" if [ -z "$KERNEL_BRANCH" ] ; then @@ -330,6 +361,11 @@ if [ "$ENABLE_VIDEOCORE" = true ] ; then REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cmake" fi +# Add deps for nexmon +if [ "$ENABLE_NEXMON" = true ] ; then + REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libgmp3-dev gawk qpdf bison flex make autoconf automake build-essential libtool" +fi + # Add libncurses5 to enable kernel menuconfig if [ "$KERNEL_MENUCONFIG" = true ] ; then REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses-dev" @@ -386,6 +422,11 @@ if [ -n "$SSH_USER_PUB_KEY" ] ; then fi fi +if [ "$ENABLE_NEXMON" = true ] && [ -n "$KERNEL_BRANCH" ] ; then + echo "error: Please unset KERNEL_BRANCH if using ENABLE_NEXMON" + exit 1 +fi + # Check if all required packages are installed on the build system for package in $REQUIRED_PACKAGES ; do if [ "$(dpkg-query -W -f='${Status}' "$package")" != "install ok installed" ] ; then @@ -442,6 +483,12 @@ if [ -n "$FBTURBOSRC_DIR" ] && [ ! -d "$FBTURBOSRC_DIR" ] ; then exit 1 fi +# Check if specified NEXMONSRC_DIR directory exists +if [ -n "$NEXMONSRC_DIR" ] && [ ! -d "$NEXMONSRC_DIR" ] ; then + echo "error: '${NEXMONSRC_DIR}' specified directory not found (NEXMONSRC_DIR)!" + exit 1 +fi + # Check if specified CHROOT_SCRIPTS directory exists if [ -n "$CHROOT_SCRIPTS" ] && [ ! -d "$CHROOT_SCRIPTS" ] ; then echo "error: ${CHROOT_SCRIPTS} specified directory not found (CHROOT_SCRIPTS)!" @@ -493,6 +540,10 @@ fi if [ "$ENABLE_IPTABLES" = true ] ; then APT_INCLUDES="${APT_INCLUDES},iptables,iptables-persistent" fi +# Add apparmor for KERNEL_SECURITY +if [ "$KERNEL_SECURITY" = true ] ; then + APT_INCLUDES="${APT_INCLUDES},apparmor,apparmor-utils,apparmor-profiles,apparmor-profiles-extra,libapparmor-perl" +fi # Add openssh server package if [ "$ENABLE_SSHD" = true ] ; then @@ -546,16 +597,6 @@ if [ "$ENABLE_SYSVINIT" = false ] ; then APT_EXCLUDES="--exclude=${APT_EXCLUDES},init,systemd-sysv" fi -# Check if kernel is getting compiled -if [ "$BUILD_KERNEL" = false ] ; then - echo "Downloading precompiled kernel" - echo "error: not configured" - exit 1; -# BUILD_KERNEL=true -else - echo "No precompiled kernel repositories were added" -fi - # Configure kernel sources if no KERNELSRC_DIR if [ "$BUILD_KERNEL" = true ] && [ -z "$KERNELSRC_DIR" ] ; then KERNELSRC_CONFIG=true