Safeguard
Application Security•

CWE-369 Divide by Zero: Small Bug, Real Denial of Service

CWE-369 is a division by a value the attacker controls, ending in a crash. Learn how zero gets in and the one line guard that stops it.

Safeguard Security Team
3 min read

CWE-369, Divide By Zero, is exactly what it says: the product divides a value by zero. It looks minor. In a service that many people depend on, a crash someone can trigger on demand is an availability problem.

It hits any code that does arithmetic on input, from image parsers to billing logic.

How it happens

MITRE explains that this typically occurs when an unexpected value reaches the product, or when an error is not properly detected. It is common in calculations involving physical dimensions such as size, length, width and height.

The observed examples show how ordinary the triggers are.

A zero in a header, an empty file, a missing field that defaults to zero. Each is easy for an attacker to supply.

What an attacker gains

MITRE lists availability: a divide by zero results in a crash. There is no data theft in the record, but repeated crashes can take a service down, and in some environments a crash restarts a privileged component.

How to find it

MITRE names two methods. Static analysis can find some instances by analyzing source code. Fuzzing generates large numbers of diverse inputs, random or algorithmic, and runs the code with them, which is well suited to hitting zero values.

Also do this by hand:

  • Search for division and modulo operators.
  • For each divisor, trace where it comes from. If any path starts at input, a file or a network field, it needs a guard.
  • Add test cases with zero, empty and missing values.

How to fix it

MITRE lists no formal mitigations. The standard answer is to validate the divisor before dividing.

Vulnerable C:

int per_item(int total, int count) {
    return total / count;      /* crashes when count is 0 */
}

Guarded:

int per_item(int total, int count, int *out) {
    if (count == 0) return -1;
    *out = total / count;
    return 0;
}

The caller then handles the error path instead of crashing. Two more habits help. Reject impossible values at the edge, so a size of zero never reaches the math. And decide what a sensible default result is when the input is empty, such as zero items meaning an average of zero, and write it down in code.

Quick check

Which test technique finds this class of bug well?

Fuzzing, because it generates many diverse inputs and will eventually produce zero or empty values in the places that divide.

What impact does MITRE list?

Availability, in the form of a crash.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.