Safeguard
Application Security•

CWE-826 Premature Release of Resource During Expected Lifetime

Releasing a resource that someone is still using is its own weakness. CWE-826 explains the risk and how to track lifetimes correctly.

Safeguard Security Team
2 min read

CWE-826 is a product releasing a resource that it, or another actor, still intends to use. Memory, file descriptors, locks, handles and connections are all candidates.

MITRE marks it as not language specific. It is closely related to CWE-825, but the failure is different: there, the release was fine and a stale pointer remained. Here, the release should not have happened at all.

How it happens

Ownership gets muddled. One component decides it is finished and closes something another component is still holding. The freed resource may be reused in the meantime.

MITRE's observed example is CVE-2009-3547, a chain where a race condition might allow a resource to be released before it is operated on, leading to a NULL dereference.

// Vulnerable: callback closes the fd the caller still uses
void on_done(int fd) { close(fd); }
void run(int fd) {
    start_async(fd, on_done);
    write(fd, "x", 1);    // may run after close, fd may be reused
}

What an attacker gains

MITRE lists reading data that now belongs to a different user or entity if the released resource is reused, crashes when the resource is no longer in the expected state, and modification of data or memory, which can extend to code execution.

How to find it

MITRE names automated static analysis. Add review of lifetimes: for every resource, write down who releases it and what else might still hold it. Stress testing with concurrency helps expose release before use.

How to fix it

The record has no formal mitigations. Practical steps:

  1. Define one owner per resource.
  2. Use reference counting or scoped ownership so release happens only when the last user is done.
  3. Synchronize release with use when multiple threads share the resource.
  4. Clear references after release.

Quick check

Why can a premature close leak another user's data?

Once released, the identifier can be reused for a different purpose. A later access through the old reference may then touch someone else's resource.

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.