github / github/codeql

Failure to detect free-memory access

未關閉
#9,417 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
question
主要語言
CodeQL
星號
10.1k
分支
2.1k
平均合併
2 天 15 小時
30 天內合併 PR
141

描述

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();
}
```

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。