#!/bin/sh
#
# Regression test for CVE-2026-107161: heap buffer overflow in the
# DIGEST-MD5 plugin's add_to_challenge() function.
#
# The harness (digestmd5-cve-2026-107161.c) plays the role of a malicious
# DIGEST-MD5 server.  It feeds a challenge whose realm value is full of
# backslashes to the SASL client API.  A vulnerable libsasl2-modules
# corrupts the heap while quoting that realm to build the client reply;
# glibc detects the corruption (abort / non-zero exit), which fails the
# test.  A fixed library completes without corruption and the test passes.

set -eu

here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT HUP INT TERM

echo "Building the DIGEST-MD5 CVE-2026-107161 regression harness"
gcc -O2 -Wall -I/usr/include/sasl -o "$work/digestmd5-cve" \
    "$here/digestmd5-cve-2026-107161.c" -lsasl2

echo "Running the DIGEST-MD5 CVE-2026-107161 regression harness"
if "$work/digestmd5-cve"; then
    echo "PASS: no heap corruption in DIGEST-MD5 add_to_challenge()"
else
    echo "FAIL: heap corruption detected in DIGEST-MD5 add_to_challenge() (CVE-2026-107161)"
    exit 1
fi
