Skip to content

A hardened Linux desktop, rebuilt from scratch, with your Windows apps built in. Free and open source.

7.6 KB
Raw Blame
#!/bin/sh
# Turns 32-bit programs on or off from the next start-up: "Steam and 32-bit
# apps" in Settings > Privacy and security, also offered by the prompt that
# appears when a 32-bit program is opened while they are off. Runs as root
# through pkexec (org.wildos.settings.32bit), and from settings-protection,
# which turns them off at Strict and Maximum.
#
# Wild OS ships with them off: Kicksecure's ia32_emulation=0 removes the
# kernel's 32-bit compatibility layer, a part of the kernel that has had
# more than its share of security bugs. Steam's client and many older games
# are 32-bit and cannot start without it. Turning it on drops that option,
# and gives 32-bit programs back their vDSO (vdso32=0 and the
# abi.vsyscall32=0 sysctl, which Kicksecure sets as a pair): without it every
# system call they make goes the slow int 0x80 way and every clock read
# becomes a full system call, a cost games pay many times a frame. 64-bit
# programs are not affected either way. The kernel decides ia32_emulation at
# boot, so it takes a restart.
#
# It also lets a program watch the programs it started itself (ptrace), which
# Kicksecure switches off for everything and which some games will not start
# without.
#
# Steam's controller rules (Debian's steam-devices) come with it. They let
# the signed-in user open game controllers directly and create virtual input
# devices through /dev/uinput, which Steam Input and Remote Play need, and
# which any program run by that user could also use to type or click. The
# package moves the rules aside (debian/preinst), and they are linked back
# into udev only while 32-bit apps are on.
#
#   on | off   save the choice and rebuild the boot menu
#   devices    link or unlink the controller rules to match the saved choice
#   ptrace     write the ptrace file on PCs that turned this on before it existed

set -eu

CHOICE=/etc/wildos/32bit
DROPIN=/etc/default/grub.d/41_wildos_32bit.cfg
# Sorts after Kicksecure's 990-security-misc.conf, so this value wins.
SYSCTL=/etc/sysctl.d/991-wildos-32bit.conf
LEVEL_FILE=/etc/wildos/protection-level
RULES_SRC=/usr/share/wildos/steam-devices
RULES_DST=/etc/udev/rules.d
RULES="60-steam-input.rules 60-steam-vr.rules"
NTSYNC=/etc/modules-load.d/wildos-ntsync.conf
# Takes the place of Kicksecure's file of the same name in /usr/lib/sysctl.d.
# A later file cannot do it: that one sets 3, and the kernel never lets 3
# be lowered again until the next start-up.
PTRACE=/etc/sysctl.d/30_security-misc_ptrace-disable.conf

# Some games and launchers (Sober, which runs Roblox, is one) watch a program
# they started themselves, and refuse to start when they can't. Kicksecure
# forbids every program from watching another (3); 1 lets a program watch
# only the ones it started, which is what Debian and Ubuntu ship. It applies
# from the next start-up, the restart the switch already asks for.
ptrace_own() {
    cat > "$PTRACE" <<'EOF'
# Written by Wild OS Settings: Steam and 32-bit apps are on, so a program may
# watch the programs it started itself. Kicksecure's own file, which this one
# stands in for, allows none.
kernel.yama.ptrace_scope = 1
EOF
}

# The start-up image carries its own copy of these settings and applies it
# before the disk is opened, so a change to the file only counts once the
# image is rebuilt. It takes a minute or two. The live session has no image
# of its own to rebuild.
start_up_image() {
    if [ ! -d /run/initramfs/live ] && command -v dracut >/dev/null 2>&1; then
        # One kernel at a time: --regenerate-all also tries the module
        # folders a removed kernel leaves behind, and fails on those.
        for kernel in /boot/vmlinuz-*; do
            [ -e "$kernel" ] || continue
            dracut --force --kver "${kernel#/boot/vmlinuz-}" >/dev/null 2>&1 ||
                echo "settings-32bit: the start-up image for ${kernel#/boot/vmlinuz-} could not be rebuilt, so it keeps the old setting" >&2
        done
    fi
}

# Controllers plugged in now pick up the change straight away. uinput is
# loaded here because udev only sees it once the module is in; at start-up
# its static node does that job. Turning off closes /dev/uinput at once (a
# mode of 0600 also masks the user's ACL entry); a controller already
# plugged in keeps its access until it is unplugged or the PC restarts.
devices() {
    for rule in $RULES; do
        if [ -e "$CHOICE" ] && [ -e "$RULES_SRC/$rule" ]; then
            ln -sfn "$RULES_SRC/$rule" "$RULES_DST/$rule"
        elif [ -L "$RULES_DST/$rule" ]; then
            rm -f "$RULES_DST/$rule"
        fi
    done
    # ntsync (Linux 6.14 and later) does Windows' locking in the kernel,
    # which Proton uses for smoother frame times in games. Loaded from the
    # next start-up on and now; left loaded on turning off, as a game may
    # still have it open. Kernels without it skip it quietly.
    if [ -e "$CHOICE" ]; then
        echo ntsync > "$NTSYNC"
        modprobe -q ntsync 2>/dev/null || true
    else
        rm -f "$NTSYNC"
    fi
    if [ -e "$CHOICE" ]; then
        modprobe -q uinput 2>/dev/null || true
    elif [ -c /dev/uinput ]; then
        chmod 0600 /dev/uinput 2>/dev/null || true
    fi
    if [ -d /run/udev ] && command -v udevadm >/dev/null 2>&1; then
        udevadm control --reload >/dev/null 2>&1 || true
        udevadm trigger --action=change --subsystem-match=misc \
            --subsystem-match=hidraw --subsystem-match=usb >/dev/null 2>&1 || true
    fi
}

case "${1:-}" in
on)
    # Strict and Maximum keep the 32-bit layer out of the kernel; Settings
    # greys the switch out there, and this refuses in case anything else asks.
    case "$(cat "$LEVEL_FILE" 2>/dev/null || true)" in
    strict | maximum)
        echo "Strict and Maximum protection keep 32-bit apps off." >&2
        exit 3
        ;;
    esac
    mkdir -p /etc/wildos
    echo on > "$CHOICE"
    # Sorts after 40_kernel_hardening.cfg, so it edits what that file added,
    # whatever an update of it changes around these two options.
    cat > "$DROPIN" <<'EOF'
# Written by Wild OS Settings: Steam and 32-bit apps are on. Every other
# kernel hardening option stays.
GRUB_CMDLINE_LINUX="$(printf '%s' "$GRUB_CMDLINE_LINUX" | sed -e 's/ ia32_emulation=0//g' -e 's/ vdso32=0//g')"
EOF
    cat > "$SYSCTL" <<'EOF'
# Written by Wild OS Settings: Steam and 32-bit apps are on, so they get the
# 32-bit vDSO back.
abi.vsyscall32 = 1
# Some games use split locks, which newer Intel processors otherwise punish
# with a delay that shows as stutter. The delay only
# guards against one program slowing others down, not against reading them.
# The dash skips it on processors without split lock detection.
-kernel.split_lock_mitigate = 0
EOF
    sysctl -q -w kernel.split_lock_mitigate=0 2>/dev/null || true
    ptrace_own
    start_up_image
    devices
    ;;
off)
    [ -e "$CHOICE" ] || [ -e "$DROPIN" ] || [ -e "$SYSCTL" ] || exit 0
    had_ptrace=no
    [ -e "$PTRACE" ] && had_ptrace=yes
    rm -f "$CHOICE" "$DROPIN" "$SYSCTL" "$PTRACE"
    [ "$had_ptrace" = yes ] && start_up_image
    sysctl -q -w abi.vsyscall32=0 2>/dev/null || true
    sysctl -q -w kernel.split_lock_mitigate=1 2>/dev/null || true
    # Raising this back takes effect at once; only lowering needs a restart.
    sysctl -q -w kernel.yama.ptrace_scope=3 2>/dev/null || true
    devices
    ;;
devices)
    devices
    exit 0
    ;;
ptrace)
    if [ -e "$CHOICE" ] && [ ! -e "$PTRACE" ]; then
        ptrace_own
        start_up_image
    fi
    exit 0
    ;;
*)
    echo "usage: $0 on|off|devices|ptrace" >&2
    exit 2
    ;;
esac

if command -v update-grub >/dev/null 2>&1; then
    update-grub >/dev/null 2>&1
else
    grub-mkconfig -o /boot/grub/grub.cfg >/dev/null 2>&1
fi