github / github/codeql

Failure to detect free-memory access

Ouverte
#9,417 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
question
Langage dominant
CodeQL
Étoiles
10.1k
Forks
2.1k
Merge moyen
2 j 15 h
PR mergées (30 j)
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();
}
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.