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 👇

7.9 KB
Raw Blame
// ©AngelaMos | 2026
// classify.rs

use libcasr::asan::AsanContext;
use libcasr::severity::Severity as CasrSeverity;
use lisdex_core::report::{self, SanitizerReport};

use crate::boundary::FaultAddress;
use crate::severity::Severity;

const DOUBLE_FREE_CLASS: &str = "double-free";
const DOUBLE_FREE_SEVERITY: Severity = Severity::ProbablyExploitable;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DecidedBy {
    Casr,
    Lisdex,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Classification {
    severity: Severity,
    class: String,
    decided_by: DecidedBy,
    fault_address: FaultAddress,
}

impl Classification {
    #[must_use]
    pub fn severity(&self) -> Severity {
        self.severity
    }

    #[must_use]
    pub fn class(&self) -> &str {
        &self.class
    }

    #[must_use]
    pub fn decided_by(&self) -> DecidedBy {
        self.decided_by
    }

    #[must_use]
    pub fn fault_address(&self) -> FaultAddress {
        self.fault_address
    }

    #[must_use]
    pub fn is_security_relevant(&self) -> bool {
        self.fault_address.is_security_relevant()
    }
}

#[must_use]
pub fn classify(text: &str) -> Option<Classification> {
    let parsed = SanitizerReport::parse(text)?;
    let fault_address = FaultAddress::of(parsed.fault_address());
    if parsed.is_undefined_behavior() {
        return Some(Classification {
            severity: Severity::Undefined,
            class: parsed.kind().to_owned(),
            decided_by: DecidedBy::Lisdex,
            fault_address,
        });
    }

    let anchored = report::anchored_lines(text)?;
    let context = AsanContext(anchored.iter().map(|line| (*line).to_owned()).collect());
    let class = context.severity().ok()?;
    let severity = Severity::from_casr(&class.severity)?;

    if class.short_description == DOUBLE_FREE_CLASS {
        return Some(Classification {
            severity: DOUBLE_FREE_SEVERITY,
            class: class.short_description,
            decided_by: DecidedBy::Lisdex,
            fault_address,
        });
    }

    Some(Classification {
        severity,
        class: class.short_description,
        decided_by: DecidedBy::Casr,
        fault_address,
    })
}

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

    const HEAP_WRITE: &str = "\
=================================================================
==53767==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7bd6bfde0020 at pc 0x558474e40fd9
WRITE of size 32 at 0x7bd6bfde0020 thread T0
    #0 0x558474e40fd8 in heap_write /work/target/doc07lab/asanlab.c:12:3
SUMMARY: AddressSanitizer: heap-buffer-overflow (/work/target/doc07lab/asanlab+0xcdfd8) in heap_write
";

    const HEAP_READ: &str = "\
=================================================================
==53768==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7bd6bfde0020 at pc 0x558474e40fd9
READ of size 1 at 0x7bd6bfde0020 thread T0
    #0 0x558474e40fd8 in heap_read /work/target/doc07lab/asanlab.c:20:10
SUMMARY: AddressSanitizer: heap-buffer-overflow (/work/target/doc07lab/asanlab+0x114a76) in heap_read
";

    const DOUBLE_FREE: &str = "\
=================================================================
==53770==ERROR: AddressSanitizer: attempting double-free on 0x7b6189de0040 in thread T0:
    #0 0x558474e42daa in free /work/target/doc07lab/asanlab.c:49:3
SUMMARY: AddressSanitizer: double-free (/work/target/doc07lab/asanlab+0xcfdaa) in free
";

    const NULL_DEREF: &str = "\
AddressSanitizer:DEADLYSIGNAL
=================================================================
==53781==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x556019fc2de9 bp 0x7ffee6a1c130 sp 0x7ffee6a1c120 T0)
==53781==The signal is caused by a READ memory access.
==53781==Hint: address points at the zero page.
    #0 0x556019fc2de8 in null_deref /work/target/doc07lab/asanlab.c:60:10
SUMMARY: AddressSanitizer: SEGV (/work/target/doc07lab/asanlab+0x114de9) in null_deref
";

    const MEMORY_LEAK: &str = "
=================================================================
==212189==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 2544 byte(s) in 32 object(s) allocated from:
    #0 0x55af16e4d048 in malloc /work/target/doc07lab/leaklab.c:10:22
    #1 0x55af16e91839 in lose_it /work/target/doc07lab/leaklab.c:10:22
SUMMARY: AddressSanitizer: 2544 byte(s) leaked in 32 allocation(s).
";

    const UBSAN_SHIFT: &str = "\
ubsanlab.c:21:12: runtime error: shift exponent 35 is too large for 32-bit type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ubsanlab.c:21:12
";

    #[test]
    fn a_write_overflow_is_exploitable_and_a_read_overflow_is_not() {
        let write = classify(HEAP_WRITE).unwrap();
        let read = classify(HEAP_READ).unwrap();
        assert_eq!(write.severity(), Severity::Exploitable);
        assert_eq!(write.class(), "heap-buffer-overflow(write)");
        assert_eq!(read.severity(), Severity::NotExploitable);
        assert_eq!(read.class(), "heap-buffer-overflow(read)");
    }

    #[test]
    fn double_free_is_overridden_above_the_taxonomy_verdict() {
        let classification = classify(DOUBLE_FREE).unwrap();
        assert_eq!(classification.class(), DOUBLE_FREE_CLASS);
        assert_eq!(
            classification.severity(),
            Severity::ProbablyExploitable,
            "libcasr calls double-free NOT_EXPLOITABLE; corrupting allocator metadata is a well-known write primitive, so the verdict is lisdex's, not the taxonomy's"
        );
        assert_eq!(classification.decided_by(), DecidedBy::Lisdex);
    }

    #[test]
    fn a_null_dereference_is_recognised_as_near_null_not_a_bare_access_violation() {
        let classification = classify(NULL_DEREF).unwrap();
        assert_eq!(
            classification.class(),
            "SourceAvNearNull",
            "libcasr reads the fault address from line 0 and the READ/WRITE qualifier from lines 1-2 only; this report carries a DEADLYSIGNAL preamble, so without anchoring the report to its header line the class degrades to a bare AccessViolation"
        );
        assert_eq!(classification.severity(), Severity::NotExploitable);
    }

    #[test]
    fn a_leak_report_is_classified_rather_than_refused() {
        assert!(
            MEMORY_LEAK.starts_with('\n'),
            "the fixture must reproduce the shape a real capture has: LeakSanitizer opens with a blank line, so it is not the first line libcasr would see"
        );
        let classification = classify(MEMORY_LEAK).unwrap();
        assert_eq!(
            classification.class(),
            "memory-leaks",
            "libcasr only reaches its LeakSanitizer branch when the header is the first line it sees, and a real captured leak report opens with a blank line"
        );
        assert_eq!(classification.severity(), Severity::NotExploitable);
    }

    #[test]
    fn undefined_behaviour_is_never_stamped_not_exploitable_by_the_taxonomy() {
        let classification = classify(UBSAN_SHIFT).unwrap();
        assert_eq!(
            classification.decided_by(),
            DecidedBy::Lisdex,
            "libcasr hardcodes NOT_EXPLOITABLE for every UBSan finding without consulting CLASSES, and a verdict of safe must never come from a path that cannot produce any other answer"
        );
        assert_eq!(
            classification.severity(),
            Severity::Undefined,
            "lisdex has no ground-truth corpus for UBSan severity, so the finding is flagged for review rather than assigned a tier it cannot defend"
        );
        assert!(
            classification.class().contains("shift exponent"),
            "the SUMMARY line says only the generic undefined-behavior, so the class must come from the runtime error description: {}",
            classification.class()
        );
    }

    #[test]
    fn text_that_is_not_a_sanitizer_report_is_refused() {
        assert!(classify("").is_none());
        assert!(classify("hello world\n").is_none());
        assert!(classify("\n\n").is_none());
    }
}