Skip to content

Commit 715f233

Browse files
committed
C++: Add a new model class describing pure memory functions, and use this new model in DefaultSafeExternalAPIFunction.
1 parent 09c5caa commit 715f233

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

cpp/ql/src/Security/CWE/CWE-020/SafeExternalAPIFunction.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ abstract class SafeExternalAPIFunction extends Function { }
1212

1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
15-
DefaultSafeExternalAPIFunction() { this.hasGlobalName(["strcmp", "strlen", "memcmp"]) }
15+
DefaultSafeExternalAPIFunction() {
16+
this instanceof PureStrFunction or
17+
this instanceof StrLenFunction or
18+
this instanceof PureMemFunction
19+
}
1620
}

cpp/ql/src/Security/CWE/CWE-020/ir/SafeExternalAPIFunction.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ abstract class SafeExternalAPIFunction extends Function { }
1212

1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
15-
DefaultSafeExternalAPIFunction() { this.hasGlobalName(["strcmp", "strlen", "memcmp"]) }
15+
DefaultSafeExternalAPIFunction() {
16+
this instanceof PureStrFunction or
17+
this instanceof StrLenFunction or
18+
this instanceof PureMemFunction
19+
}
1620
}

cpp/ql/src/semmle/code/cpp/models/implementations/Pure.qll

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semmle.code.cpp.models.interfaces.Taint
33
import semmle.code.cpp.models.interfaces.Alias
44
import semmle.code.cpp.models.interfaces.SideEffect
55

6+
/** Pure string functions. */
67
class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunction, SideEffectFunction {
78
PureStrFunction() {
89
hasGlobalOrStdName([
@@ -58,6 +59,7 @@ class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunction, SideE
5859
}
5960
}
6061

62+
/** String standard `strlen` function, and related functions for computing string lengths. */
6163
class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFunction {
6264
StrLenFunction() {
6365
hasGlobalOrStdName(["strlen", "strnlen", "wcslen"])
@@ -91,6 +93,7 @@ class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFunction {
9193
}
9294
}
9395

96+
/** Pure functions. */
9497
class PureFunction extends TaintFunction, SideEffectFunction {
9598
PureFunction() { hasGlobalOrStdName(["abs", "labs"]) }
9699

@@ -106,3 +109,49 @@ class PureFunction extends TaintFunction, SideEffectFunction {
106109

107110
override predicate hasOnlySpecificWriteSideEffects() { any() }
108111
}
112+
113+
/** Pure raw-memory functions. */
114+
class PureMemFunction extends AliasFunction, ArrayFunction, TaintFunction, SideEffectFunction {
115+
PureMemFunction() { hasGlobalOrStdName(["memchr", "memrchr", "rawmemchr", "memcmp", "memmem"]) }
116+
117+
override predicate hasArrayInput(int bufParam) {
118+
getParameter(bufParam).getUnspecifiedType() instanceof PointerType
119+
}
120+
121+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
122+
exists(ParameterIndex i |
123+
input.isParameter(i) and
124+
exists(getParameter(i))
125+
or
126+
input.isParameterDeref(i) and
127+
getParameter(i).getUnspecifiedType() instanceof PointerType
128+
) and
129+
(
130+
output.isReturnValueDeref() and
131+
getUnspecifiedType() instanceof PointerType
132+
or
133+
output.isReturnValue()
134+
)
135+
}
136+
137+
override predicate parameterNeverEscapes(int i) {
138+
getParameter(i).getUnspecifiedType() instanceof PointerType and
139+
not parameterEscapesOnlyViaReturn(i)
140+
}
141+
142+
override predicate parameterEscapesOnlyViaReturn(int i) {
143+
i = 0 and
144+
getUnspecifiedType() instanceof PointerType
145+
}
146+
147+
override predicate parameterIsAlwaysReturned(int i) { none() }
148+
149+
override predicate hasOnlySpecificReadSideEffects() { any() }
150+
151+
override predicate hasOnlySpecificWriteSideEffects() { any() }
152+
153+
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) {
154+
getParameter(i).getUnspecifiedType() instanceof PointerType and
155+
buffer = true
156+
}
157+
}

0 commit comments

Comments
 (0)