Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

5.9 KB
Raw Blame
// ©AngelaMos | 2026
// bundle.rs

use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use lisdex_core::config::{
    ENV_ASAN_OPTIONS, ENV_UBSAN_OPTIONS, REPRODUCE_SANITIZER_OPTIONS,
    REPRODUCE_SANITIZER_OPTIONS_WITH_LEAKS,
};

use crate::advisory::Advisory;
use crate::{json, markdown};

pub const ADVISORY_MARKDOWN: &str = "advisory.md";
pub const ADVISORY_JSON: &str = "advisory.json";
pub const REPRODUCER: &str = "poc";
pub const SANITIZER_REPORT: &str = "report.txt";
pub const REPRODUCE_SCRIPT: &str = "reproduce.sh";

const LEAK_CLASS: &str = "memory-leaks";
const SCRIPT_MODE: u32 = 0o755;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reproducer {
    Minimized,
    Original,
}

impl Reproducer {
    #[must_use]
    pub fn note(self) -> &'static str {
        match self {
            Self::Minimized => {
                "This input was reduced by afl-tmin and lisdex re-ran it to confirm it still \
                 produces the same stack signature as the crash it came from."
            }
            Self::Original => {
                "This is the original crashing input as the fuzzer saved it, not a reduced one. \
                 lisdex only ships a reduced input when it has re-run it and confirmed the bug did \
                 not change underneath the reduction."
            }
        }
    }
}

#[must_use]
pub fn sanitizer_options(class: &str) -> &'static str {
    if class == LEAK_CLASS {
        REPRODUCE_SANITIZER_OPTIONS_WITH_LEAKS
    } else {
        REPRODUCE_SANITIZER_OPTIONS
    }
}

#[must_use]
pub fn reproduce_script(class: &str) -> String {
    let options = sanitizer_options(class);
    let mut script = String::new();
    let _ = write!(
        script,
        "#!/bin/sh\n\
         set -eu\n\
         if [ $# -ne 1 ]; then\n\
         \x20   echo \"usage: $0 <path to the built target>\" >&2\n\
         \x20   exit 2\n\
         fi\n\
         {ENV_ASAN_OPTIONS}='{options}'\n\
         {ENV_UBSAN_OPTIONS}='{options}'\n\
         export {ENV_ASAN_OPTIONS} {ENV_UBSAN_OPTIONS}\n\
         exec \"$1\" \"$(dirname \"$0\")/{REPRODUCER}\"\n"
    );
    script
}

fn make_executable(path: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut permissions = std::fs::metadata(path)?.permissions();
        permissions.set_mode(SCRIPT_MODE);
        std::fs::set_permissions(path, permissions)?;
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}

pub fn write(
    dir: &Path,
    advisory: &Advisory,
    input: &[u8],
    reproducer: Reproducer,
) -> std::io::Result<Vec<PathBuf>> {
    std::fs::create_dir_all(dir)?;

    let markdown_path = dir.join(ADVISORY_MARKDOWN);
    let json_path = dir.join(ADVISORY_JSON);
    let poc_path = dir.join(REPRODUCER);
    let report_path = dir.join(SANITIZER_REPORT);
    let script_path = dir.join(REPRODUCE_SCRIPT);

    std::fs::write(&markdown_path, markdown::render(advisory, Some(reproducer)))?;
    std::fs::write(&json_path, json::render_string(advisory))?;
    std::fs::write(&poc_path, input)?;
    std::fs::write(&report_path, advisory.sanitizer_report())?;
    std::fs::write(&script_path, reproduce_script(advisory.class()))?;
    make_executable(&script_path)?;

    Ok(vec![
        markdown_path,
        json_path,
        poc_path,
        report_path,
        script_path,
    ])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_script_carries_the_same_options_the_reproduce_path_uses() {
        let script = reproduce_script("heap-buffer-overflow(write)");
        assert!(
            script.contains(REPRODUCE_SANITIZER_OPTIONS),
            "the bundle documents how to reproduce a crash lisdex produced, so it has to name the options lisdex actually ran under; a command that differs from the one that produced the report may simply not crash for the maintainer"
        );
    }

    #[test]
    fn a_leak_finding_is_reproduced_with_leak_detection_switched_on() {
        assert_eq!(
            sanitizer_options(LEAK_CLASS),
            REPRODUCE_SANITIZER_OPTIONS_WITH_LEAKS
        );
        assert!(
            REPRODUCE_SANITIZER_OPTIONS.contains("detect_leaks=0"),
            "this is the whole reason the leak case needs its own options: the campaign reproduce path disables leak detection, so shipping it for a leak finding would hand over a command that reports nothing"
        );
        assert!(
            reproduce_script(LEAK_CLASS).contains("detect_leaks=1"),
            "and the emitted script has to carry the corrected options, not just the helper"
        );
    }

    #[test]
    fn every_other_class_keeps_the_campaign_options() {
        for class in [
            "heap-buffer-overflow(read)",
            "heap-use-after-free(write)",
            "double-free",
            "SourceAvNearNull",
            "",
        ] {
            assert_eq!(sanitizer_options(class), REPRODUCE_SANITIZER_OPTIONS);
        }
    }

    #[test]
    fn the_script_takes_the_target_from_the_caller_and_the_input_from_beside_itself() {
        let script = reproduce_script("double-free");
        assert!(
            script.contains("exec \"$1\""),
            "lisdex fuzzed a build at a path that means nothing on the maintainer's machine, so the target has to be an argument"
        );
        assert!(
            script.contains("dirname"),
            "the reproducer travels with the script, so the script must find it relative to itself rather than the caller's working directory"
        );
        assert!(script.starts_with("#!/bin/sh\nset -eu\n"));
    }

    #[test]
    fn the_two_reproducer_kinds_say_different_things_about_what_was_verified() {
        assert_ne!(
            Reproducer::Minimized.note(),
            Reproducer::Original.note(),
            "shipping an unreduced input under a note claiming its identity was verified would overstate what lisdex checked"
        );
        assert!(Reproducer::Original.note().contains("not a reduced one"));
    }
}