github / github/codeql

how to filter out this situation?

未关闭
#19,838 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
question
主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 15 小时
30 天内合并 PR
141

描述

Hi, I have been learning to use CodeQL recently. I was trying to find all expressions that reach the `len` parameter of `memcpy`, and in the results, there is a case like the following.

```c
attr->val.octets = _malloc(attr->length);
if (!attr->val.octets)
goto out_err_mem;
memcpy(attr->val.octets, orig_avp_val, attr->length);
```
In this part, the `len` parameter of `memcpy` is exactly the same as the parameter of `_malloc`, which I consider to be safe. Therefore, I would like to exclude this situation. However, first, I want to identify this pattern, so I have written the following code.

```ql
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
import semmle.code.cpp.dataflow.new.TaintTracking

// class MallocSize extends Expr {
// MallocSize() {
// exists(FunctionCall fc |
// fc.getTarget().hasName("malloc") and
// this = fc.getArgument(0)
// )
// }
// }

module RecvToMemcpyConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
exists(Expr e | source.asExpr() = e
and not e.isConstant()
)
}

predicate isSink(DataFlow::Node sink) {
exists(FunctionCall fc, FunctionCall mc |
fc.getTarget().hasName("memcpy") and mc.getTarget().hasName("malloc")
and sink.asExpr() = fc.getArgument(2)
and fc.getArgument(2) = mc.getArgument(0)
)
and not sink.asExpr().isConstant()
}
}

module RecvToMemcpyFlow = TaintTracking::Global;

from RecvToMemcpyFlow::PathNode source, RecvToMemcpyFlow::PathNode sink
where RecvToMemcpyFlow::flowPath(source, sink)
select
source,
sink,
sink.getNode().getFunction().getFile(),
source.getNode().getFunction().getFile()
```

Although I think this code may not handle the following situation, I believe it should be able to handle the case where the `size` parameter of `malloc` and the `len` parameter of `memcpy` are exactly the same, meaning when the expressions are an exact match.

```c
b = _malloc(sizeof(*b) + size);
b->size = size;
memcpy(b->buf, buf, size);
```

But the result returns 0 results.

I also tried other approaches, but none of them met my expectations. How should I correctly handle this situation?

贡献指南

打开贡献指南

调研方向

从 DataFlow 和 TaintTracking 的导入开始,然后检查 RecvToMemcpyConfig,尤其是 isSource、isSink 和 flowPath 查询。在两个 C 示例上复现该查询,并确定是否返回了预期的精确表达式和相关表达式案例。完成标准是该查询能够识别 issue 所描述的安全 malloc/memcpy 大小关系。

由索引模型根据 Issue 内容生成。

评估

技术栈
cpp
领域
devtools, security
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。