github / github/codeql

Failure to detect free-memory access

Open
#9,417 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

I have written this query to detect accessing freed pointers using `delete` expression.
```
/**
* @name Use after free
* @id cpp/sei-cert/mem/50
* @kind path-problem
* @problem.severity error
* @tags sei-security
*/
import cpp
import semmle.code.cpp.dataflow.DataFlow
import DataFlow::PathGraph

class Config extends DataFlow::Configuration {
Config() { this = "Get freed memory access." }

override predicate isSource(DataFlow::Node arg) {
exists(FunctionCall call |
call.getArgument(0) = arg.asDefiningArgument() and
call.getTarget().hasGlobalOrStdName("free") )
or
exists( DeleteExpr delexp | delexp.getExpr() = arg.asExpr() )
}

override predicate isSink(DataFlow::Node sink) {
exists( | dereferenced(sink.asExpr()) )
}
}

from Config config, DataFlow::PathNode source, DataFlow::PathNode sink
where config.hasFlowPath(source, sink)
select sink, source, sink, "Memory is $@ and $@, causing a potential vulnerability.", source, "freed here", sink, "used here"
```

and it failed to detect this code snippet even though `ncg0` function frees the `s` pointer then uses it in the same context (i.e. not freed in another function call).

```
struct ncS1 {
void f() {};
};

void ncg0() noexcept(false) {
ncS1 *s = new ncS1;
// ...
delete s;
// ...
s->f();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.