Safeguard
Application Security•

CWE-440 Expected Behavior Violation: When Code Ignores Its Spec

A feature, API or function that does not do what its specification says. CWE-440 explained, with observed examples and test-based defenses.

Safeguard Security Team
2 min read

CWE-440, Expected Behavior Violation, is simple to state. A feature, API or function does not perform according to its specification.

Callers trust the documented contract. If the implementation drifts from it, every caller inherits the gap.

How it happens

The cause is any mismatch between spec and code: a misread requirement, a shortcut, an unhandled edge case, or an inconsistency between two implementations of the same idea. MITRE's observed examples include:

  • CVE-2003-0187: a program used large timeouts on unconfirmed connections because of an inconsistency in linked list implementations.
  • CVE-2003-0465: strncpy in the Linux kernel acts differently from libc on x86.

MITRE also shows a hardware example from the Hack@DAC 21 OpenPiton SoC, where a RISC-V register module has to implement registers according to the specification.

What an attacker gains

The record lists quality degradation, varying by context. What an attacker gets depends on what the broken behavior guarded. If a boundary or a check was part of the spec, they get past it.

How to find it

Compare behavior against the written specification. Write tests from the spec, not from the implementation, and include edge cases and error paths. Differential testing against a reference implementation can expose differences quickly.

How to fix it

MITRE lists no formal mitigations. Practical steps:

  1. Keep a clear, testable specification.
  2. Turn each security-relevant sentence in the spec into a test.
  3. Review code against the spec, especially for parsing, limits and timeouts.

Quick check

How is this different from a plain bug?

A specification exists and the code departs from it. That makes the gap measurable, which is why spec-driven tests catch it well.

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.