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.7 KB
Raw Blame
// ©AngelaMos | 2026
// weakness.rs

const QUALIFIER_OPEN: char = '(';
const QUALIFIER_CLOSE: char = ')';
const READ: &str = "read";
const WRITE: &str = "write";

const HEAP_OVERFLOW: &str = "heap-buffer-overflow";
const STACK_OVERFLOW: &str = "stack-buffer-overflow";
const GLOBAL_OVERFLOW: &str = "global-buffer-overflow";
const DYNAMIC_STACK_OVERFLOW: &str = "dynamic-stack-buffer-overflow";
const USE_AFTER_FREE_CLASS: &str = "heap-use-after-free";
const DOUBLE_FREE_CLASS: &str = "double-free";
const MEMORY_LEAKS_CLASS: &str = "memory-leaks";
const SOURCE_AV_NEAR_NULL: &str = "SourceAvNearNull";
const DEST_AV_NEAR_NULL: &str = "DestAvNearNull";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Weakness {
    pub id: &'static str,
    pub name: &'static str,
}

pub const OUT_OF_BOUNDS_WRITE: Weakness = Weakness {
    id: "CWE-787",
    name: "Out-of-bounds Write",
};

pub const STACK_BASED_BUFFER_OVERFLOW: Weakness = Weakness {
    id: "CWE-121",
    name: "Stack-based Buffer Overflow",
};

pub const HEAP_BASED_BUFFER_OVERFLOW: Weakness = Weakness {
    id: "CWE-122",
    name: "Heap-based Buffer Overflow",
};

pub const OUT_OF_BOUNDS_READ: Weakness = Weakness {
    id: "CWE-125",
    name: "Out-of-bounds Read",
};

pub const USE_AFTER_FREE: Weakness = Weakness {
    id: "CWE-416",
    name: "Use After Free",
};

pub const DOUBLE_FREE: Weakness = Weakness {
    id: "CWE-415",
    name: "Double Free",
};

pub const NULL_POINTER_DEREFERENCE: Weakness = Weakness {
    id: "CWE-476",
    name: "NULL Pointer Dereference",
};

pub const MISSING_RELEASE_OF_MEMORY: Weakness = Weakness {
    id: "CWE-401",
    name: "Missing Release of Memory after Effective Lifetime",
};

#[must_use]
pub fn split_qualifier(class: &str) -> (&str, Option<&str>) {
    match class.split_once(QUALIFIER_OPEN) {
        Some((base, rest)) => (base, rest.strip_suffix(QUALIFIER_CLOSE)),
        None => (class, None),
    }
}

#[must_use]
pub fn weakness_for(class: &str) -> Option<Weakness> {
    let (base, qualifier) = split_qualifier(class);
    match (base, qualifier) {
        (_, Some(READ))
            if matches!(
                base,
                HEAP_OVERFLOW | STACK_OVERFLOW | GLOBAL_OVERFLOW | DYNAMIC_STACK_OVERFLOW
            ) =>
        {
            Some(OUT_OF_BOUNDS_READ)
        }
        (HEAP_OVERFLOW, Some(WRITE)) => Some(HEAP_BASED_BUFFER_OVERFLOW),
        (STACK_OVERFLOW | DYNAMIC_STACK_OVERFLOW, Some(WRITE)) => Some(STACK_BASED_BUFFER_OVERFLOW),
        (GLOBAL_OVERFLOW, Some(WRITE)) => Some(OUT_OF_BOUNDS_WRITE),
        (USE_AFTER_FREE_CLASS, _) => Some(USE_AFTER_FREE),
        (DOUBLE_FREE_CLASS, None) => Some(DOUBLE_FREE),
        (MEMORY_LEAKS_CLASS, None) => Some(MISSING_RELEASE_OF_MEMORY),
        (SOURCE_AV_NEAR_NULL | DEST_AV_NEAR_NULL, None) => Some(NULL_POINTER_DEREFERENCE),
        _ => None,
    }
}

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

    #[test]
    fn the_access_direction_decides_the_weakness_not_the_region() {
        assert_eq!(
            weakness_for("heap-buffer-overflow(write)"),
            Some(HEAP_BASED_BUFFER_OVERFLOW)
        );
        assert_eq!(
            weakness_for("heap-buffer-overflow(read)"),
            Some(OUT_OF_BOUNDS_READ),
            "CWE-122 sits under CWE-787 Out-of-bounds WRITE, so calling a read overflow a heap-based buffer overflow states the wrong weakness in an advisory"
        );
        assert_eq!(
            weakness_for("stack-buffer-overflow(read)"),
            Some(OUT_OF_BOUNDS_READ)
        );
        assert_eq!(
            weakness_for("global-buffer-overflow(read)"),
            Some(OUT_OF_BOUNDS_READ)
        );
    }

    #[test]
    fn each_overflow_region_maps_to_its_own_write_weakness() {
        assert_eq!(
            weakness_for("stack-buffer-overflow(write)"),
            Some(STACK_BASED_BUFFER_OVERFLOW)
        );
        assert_eq!(
            weakness_for("dynamic-stack-buffer-overflow(write)"),
            Some(STACK_BASED_BUFFER_OVERFLOW)
        );
        assert_eq!(
            weakness_for("global-buffer-overflow(write)"),
            Some(OUT_OF_BOUNDS_WRITE),
            "CWE has no global-buffer variant, so the honest answer is the Base weakness rather than a stack or heap variant that names the wrong region"
        );
    }

    #[test]
    fn a_use_after_free_maps_the_same_way_in_both_directions() {
        assert_eq!(
            weakness_for("heap-use-after-free(read)"),
            Some(USE_AFTER_FREE)
        );
        assert_eq!(
            weakness_for("heap-use-after-free(write)"),
            Some(USE_AFTER_FREE)
        );
    }

    #[test]
    fn the_unqualified_classes_map_to_their_own_weaknesses() {
        assert_eq!(weakness_for("double-free"), Some(DOUBLE_FREE));
        assert_eq!(
            weakness_for("memory-leaks"),
            Some(MISSING_RELEASE_OF_MEMORY)
        );
        assert_eq!(
            weakness_for("SourceAvNearNull"),
            Some(NULL_POINTER_DEREFERENCE)
        );
        assert_eq!(
            weakness_for("DestAvNearNull"),
            Some(NULL_POINTER_DEREFERENCE)
        );
    }

    #[test]
    fn a_fault_that_is_not_near_null_is_refused_rather_than_called_a_null_dereference() {
        assert_eq!(
            weakness_for("SourceAv"),
            None,
            "a wild read at an address the boundary calls far is not a null dereference and is not demonstrably an out-of-bounds access either; naming a weakness here would be a guess in a security advisory"
        );
        assert_eq!(weakness_for("DestAv"), None);
        assert_eq!(weakness_for("AccessViolation"), None);
    }

    #[test]
    fn an_overflow_whose_access_direction_is_unknown_is_refused_rather_than_assumed_a_write() {
        for class in [
            "heap-buffer-overflow",
            "stack-buffer-overflow",
            "global-buffer-overflow",
            "dynamic-stack-buffer-overflow",
        ] {
            assert_eq!(
                weakness_for(class),
                None,
                "{class} carries no access direction, which is what an unanchored report produces when libcasr cannot find the READ/WRITE line; guessing WRITE here would publish CWE-787 for what may be an out-of-bounds read"
            );
        }
    }

    #[test]
    fn a_class_the_table_does_not_cover_yields_no_weakness() {
        assert_eq!(weakness_for(""), None);
        assert_eq!(weakness_for("undefined-behavior"), None);
        assert_eq!(weakness_for("shift exponent 35 is too large"), None);
        assert_eq!(weakness_for("some-future-sanitizer-class(write)"), None);
    }

    #[test]
    fn every_identifier_is_a_cwe_id_and_every_name_is_the_one_mitre_publishes() {
        for weakness in [
            OUT_OF_BOUNDS_WRITE,
            STACK_BASED_BUFFER_OVERFLOW,
            HEAP_BASED_BUFFER_OVERFLOW,
            OUT_OF_BOUNDS_READ,
            USE_AFTER_FREE,
            DOUBLE_FREE,
            NULL_POINTER_DEREFERENCE,
            MISSING_RELEASE_OF_MEMORY,
        ] {
            assert!(weakness.id.starts_with("CWE-"));
            assert!(weakness.id[4..].chars().all(|c| c.is_ascii_digit()));
            assert!(!weakness.name.is_empty());
        }
        assert_eq!(
            MISSING_RELEASE_OF_MEMORY.name, "Missing Release of Memory after Effective Lifetime",
            "the shorthand everyone writes is 'Memory Leak', which is not what CWE-401 is called, and an advisory quoting a CWE has to quote its published name"
        );
    }

    #[test]
    fn a_qualifier_is_split_off_only_when_it_is_closed() {
        assert_eq!(split_qualifier("double-free"), ("double-free", None));
        assert_eq!(
            split_qualifier("heap-buffer-overflow(write)"),
            ("heap-buffer-overflow", Some("write"))
        );
        assert_eq!(split_qualifier("truncated(write"), ("truncated", None));
    }
}