diff --git a/Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg b/Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg index 9f5fa7a9..fc476597 100755 --- a/Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg +++ b/Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg @@ -191,14 +191,22 @@ fogLogin() { [[ -n $fogAuthed ]] && return 0 local ret="" local retry=3 + # Local, so the plaintext password does not outlive this function. + local username="" + local password="" while [[ ! $retry -eq 0 ]]; do echo " * Enter FOG GUI Username and Password" echo -n " Username: " - read username + # read trims leading and trailing blanks, as the web UI trims every + # posted value, so the two agree on what the password is. + read -r username echo -n " Password: " - read -s password - user64=$(echo $username | tr -d '\012' | base64) - pass64=$(echo $password | tr -d '\012' | base64) + read -rs password + # Quoted printf, not a bare echo: word splitting and globbing changed + # a password holding spaces, '*' or '?', or one spelled -n or -e, so a + # correct login read as invalid. The tr joins base64's 76-column wrap. + user64=$(printf '%s' "$username" | base64 | tr -d '\n') + pass64=$(printf '%s' "$password" | base64 | tr -d '\n') callServer "${web}service/checkcredentials.php" "sysuuid=${sysuuid}&mac=$mac&username=$user64&password=$pass64" ret="${serverBody:-$serverReason}" case $ret in @@ -400,6 +408,8 @@ while [[ -z $askme ]]; do if fogLogin; then echo " * This host will reboot and deployment will start!" realdoimage=$(echo -n 1 | base64) + else + echo " * This host will be registered without deploying an image." fi askme="Y" ;; diff --git a/tests/checks/man-reg-login.sh b/tests/checks/man-reg-login.sh new file mode 100644 index 00000000..8e982018 --- /dev/null +++ b/tests/checks/man-reg-login.sh @@ -0,0 +1,109 @@ +#!/bin/bash +# +# Assertion harness for fogLogin() in bin/fog.man.reg. +# +# tests/checks/man-reg-login.sh [path/to/fog.man.reg] +# +# The registration screen asks for a FOG GUI login before domain join and +# before imaging. The server copies the domain join account onto a new host +# only for a signed-in operator (FOGProject/fogproject#2054, GHSA-97vv-f7hc-rp5m), +# so what this function sends and keeps is security relevant: +# +# - a password with spaces, glob characters, a backslash, or spelled -n +# reaches the server byte for byte; +# - one accepted login is not asked for again; +# - three rejections return 1 and leave no credential for the POST; +# - the plaintext password does not outlive the function. +# +# fog.man.reg is a top-level script, not a library, so the function is cut +# out of it by its own braces and evaluated alone. callServer is stubbed: it +# records each body it is given and answers from a queue. +# +# Pass an older fog.man.reg to watch this fail on unfixed code. + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="${1:-$HERE/../../Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg}" +[[ -f $SCRIPT ]] || { echo "ERROR: cannot find $SCRIPT" >&2; exit 2; } + +body=$(awk '/^fogLogin\(\) \{$/ {on=1} on {print} on && /^\}$/ {exit}' "$SCRIPT") +if [[ -z $body ]]; then + echo "FAIL: no fogLogin() in $SCRIPT" + exit 1 +fi +eval "$body" + +fails=0 +check() { + if [[ $1 == "$2" ]]; then + return 0 + fi + echo "FAIL: $3" + echo " expected: $2" + echo " got: $1" + fails=$((fails + 1)) +} + +# Stub. ANSWERS is the queue of replies; CALLS counts requests; LASTBODY is +# the most recent POST body. +callServer() { + CALLS=$((CALLS + 1)) + LASTBODY="$2" + serverBody="${ANSWERS[0]}" + serverReason="" + ANSWERS=("${ANSWERS[@]:1}") +} +reset() { + fogAuthed="" + user64="" + pass64="" + username="" + password="" + CALLS=0 + LASTBODY="" +} + +# ---- 1. awkward password, accepted first time -------------------------------- +reset +ANSWERS=('#!ok') +# Inner runs of spaces, glob characters and a backslash must survive. Edge +# blanks are trimmed on purpose: the web UI trims every posted value, so a +# stored password never has them. +pw='a b*? \x -n' +fogLogin >/dev/null <<<"fog admin"$'\n'" $pw " +rc=$? +check "$rc" 0 'an accepted login returns 0' +check "$fogAuthed" 1 'an accepted login sets fogAuthed' +check "$(printf '%s' "$pass64" | base64 -d)" "$pw" 'the password reaches the server byte for byte, edge blanks trimmed' +check "$(printf '%s' "$user64" | base64 -d)" 'fog admin' 'the username reaches the server byte for byte' +check "$password" '' 'the plaintext password does not outlive fogLogin()' +check "$username" '' 'the plaintext username does not outlive fogLogin()' + +# ---- 2. asked once ---------------------------------------------------------- +before=$CALLS +fogLogin >/dev/null /dev/null <<<$'u\np\nu\np\nu\np' +rc=$? +check "$rc" 1 'three rejections return 1' +check "$CALLS" 3 'three rejections make exactly three tries' +check "$fogAuthed" '' 'three rejections leave fogAuthed unset' +check "$user64$pass64" '' 'three rejections leave no credential for the registration POST' + +# ---- 4. a long password is one base64 line ---------------------------------- +reset +ANSWERS=('#!ok') +long=$(printf 'x%.0s' {1..100}) +fogLogin >/dev/null <<<"u"$'\n'"$long" +check "$(printf '%s' "$pass64" | tr -cd '\n' | wc -c | tr -d ' ')" 0 'a long password encodes to one line' +check "$(printf '%s' "$pass64" | base64 -d)" "$long" 'a long password reaches the server intact' + +if [[ $fails -gt 0 ]]; then + exit 1 +fi +echo "PASS man-reg-login" +exit 0