跳到主要内容

xmlXPathFreeContext

API 概述

xmlXPathFreeContextlibxml2 中的一个API。该规属于api pair 类型。该规则是使用 GPTAid 生成的。

规则描述

提示

Parameter 1 must not be used later.

信息

标签:api pair

参数下标:0

CWE类别:CWE-416

规则代码

/**
* @name UAF
* @description description
* @kind problem
* @problem.severity error
* @precision high
* @id cpp/UAF
* @tags security
*/

import cpp
import semmle.code.cpp.dataflow.TaintTracking
import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.security.Security
import semmle.code.cpp.controlflow.Guards
import semmle.code.cpp.valuenumbering.GlobalValueNumbering

Expr getMallocExpr(FunctionCall fc)
{
exists(Expr e |
result = e
and
(
(fc.getTarget().hasName("malloc") and e = fc)
or (fc.getTarget().hasName("xmlSplitQName2") and e = fc.getArgument(1))
// or
// (fc.getTarget().hasName("new_malloc") and e = fc.getArgument(0))
// TODO-addMallocHere
)
)
}

Expr getFreeExpr(FunctionCall fc)
{

result = fc.getArgument(0)
and
(
// TODO-Target-change
fc.getTarget().hasName("xmlXPathFreeContext")
// or
// fc.getTarget().hasName("new_free")

)
}
predicate isSourceFC(FunctionCall fc)
{
// fc.getTarget().hasName("new_malloc")
// or
// // TODO-addMallocFCHere
fc.getTarget().hasName("malloc")
or fc.getTarget().hasName("xmlSplitQName2")
}

predicate isSinkFC(FunctionCall fc)
{
fc.getTarget().hasName("xmlXPathFreeContext")
// or
// fc.getTarget().hasName("new_free")
}

DataFlow::Node getSourceNode(FunctionCall fc)
{
result.asExpr() = getMallocExpr(fc)
or
result.asDefiningArgument() = getMallocExpr(fc)
}
class MallocConfiguration extends DataFlow::Configuration {
MallocConfiguration() { this = "MallocConfiguration" }

override predicate isSource(DataFlow::Node source) {
exists(FunctionCall fc |
isSourceFC(fc)
and
source = getSourceNode(fc)
)
or
exists(AssignExpr ae|
ae.getAChild() = source.asExpr()
or ae.getAChild() = source.asDefiningArgument()
)
}
override predicate isSink(DataFlow::Node sink) {
// sink.asExpr()
exists(Expr e |
sink.asExpr() = e
or sink.asDefiningArgument() = e
)
}
}
// target is a free function
from FunctionCall target, Expr use
where
isSinkFC(target)
and exists(FunctionCall malloc | isSourceFC(malloc) and target.getAPredecessor*() = malloc)
and not target.getAnArgument() = use
and target.getASuccessor*() = use
// and
// isLocalVariable(getMallocExpr(target))
and exists(Variable v|
v.getAnAccess() = use
and v.getAnAccess() = getFreeExpr(target)
and not exists(Expr malloc, MallocConfiguration cfg |
use.getAPredecessor*() = malloc
and malloc.getAPredecessor*() = target
and
cfg.hasFlow(DataFlow::exprNode(malloc), DataFlow::exprNode(use))
)
)

select target, "Freed in " + target.getLocation().toString() + ". Used in " + use.getLocation().toString()