Skip to content

Commit bdd6965

Browse files
authored
Merge branch 'master' into moremsalloc
2 parents a319356 + 702fc80 commit bdd6965

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1145
-98
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
| Mismatching new/free or malloc/delete (`cpp/new-free-mismatch`) | Fewer false positive results | Fixed an issue where functions were being identified as allocation functions inappropriately. Also affects `cpp/new-array-delete-mismatch` and `cpp/new-delete-array-mismatch`. |
1515
| Memory may not be freed (`cpp/memory-may-not-be-freed`) | More correct results | Support added for more Microsoft-specific allocation functions, including `LocalAlloc`, `GlobalAlloc`, `HeapAlloc` and `CoTaskMemAlloc`. |
1616
| Memory is never freed (`cpp/memory-never-freed`) | More correct results | Support added for more Microsoft-specific allocation functions, including `LocalAlloc`, `GlobalAlloc`, `HeapAlloc` and `CoTaskMemAlloc`. |
17+
| Resource not released in destructor (`cpp/resource-not-released-in-destructor`) | Fewer false positive results | Resource allocation and deallocation functions are now determined more accurately. |
1718

1819
## Changes to QL libraries

cpp/config/suites/security/cwe-119

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@name Call to memory access function may overflow buffer (CWE-119)
44
+ semmlecode-cpp-queries/Critical/OverflowStatic.ql: /CWE/CWE-119
55
@name Static array access may cause overflow (CWE-119)
6-
# + semmlecode-cpp-queries/Critical/OverflowDestination.ql: /CWE/CWE-119
7-
# ^ disabled due to timeout issue
6+
+ semmlecode-cpp-queries/Critical/OverflowDestination.ql: /CWE/CWE-119
7+
@name Copy function using source size (CWE-119)
88
+ semmlecode-cpp-queries/Likely Bugs/Memory Management/SuspiciousCallToStrncat.ql: /CWE/CWE-119
99
@name Potentially unsafe call to strncat (CWE-119)
1010
+ semmlecode-cpp-queries/Likely Bugs/Memory Management/StrncpyFlippedArgs.ql: /CWE/CWE-119

cpp/ql/src/jsf/4.10 Classes/AV Rule 79.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ predicate acquireExpr(Expr acquire, string kind) {
2121
exists(FunctionCall fc, Function f, string name |
2222
fc = acquire and
2323
f = fc.getTarget() and
24-
name = f.getName() and
24+
name = f.getQualifiedName() and
2525
(
2626
(
2727
name = "fopen" and
@@ -47,7 +47,7 @@ predicate releaseExpr(Expr release, Expr resource, string kind) {
4747
exists(FunctionCall fc, Function f, string name |
4848
fc = release and
4949
f = fc.getTarget() and
50-
name = f.getName() and
50+
name = f.getQualifiedName() and
5151
(
5252
(
5353
name = "fclose" and

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 79/Variants.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,39 @@ class MyClass6
7373

7474
int *a, *b, *c;
7575
};
76+
77+
class MyClass7
78+
{
79+
public:
80+
MyClass7()
81+
{
82+
}
83+
84+
bool open()
85+
{
86+
// ...
87+
}
88+
89+
void close()
90+
{
91+
// ...
92+
}
93+
};
94+
95+
class myClass7Test
96+
{
97+
public:
98+
myClass7Test()
99+
{
100+
success = mc7.open(); // GOOD
101+
}
102+
103+
~myClass7Test()
104+
{
105+
mc7.close();
106+
}
107+
108+
private:
109+
MyClass7 mc7;
110+
bool success;
111+
};

csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ public Assembly(Context cx) : base(cx)
3737
if (!def.PublicKey.IsNil)
3838
assemblyName.SetPublicKey(cx.mdReader.GetBlobBytes(def.PublicKey));
3939

40-
ShortId = cx.GetId(assemblyName.FullName) + "#file:///" + cx.assemblyPath.Replace("\\", "/");
40+
ShortId = cx.GetId(FullName) + "#file:///" + cx.assemblyPath.Replace("\\", "/");
4141

4242
file = new File(cx, cx.assemblyPath);
4343
}
4444

4545
static readonly Id suffix = new StringId(";assembly");
4646

47+
string FullName => assemblyName.GetPublicKey() is null ? assemblyName.FullName + ", PublicKeyToken=null" : assemblyName.FullName;
48+
4749
public override IEnumerable<IExtractionProduct> Contents
4850
{
4951
get
5052
{
5153
yield return file;
52-
yield return Tuples.assemblies(this, file, assemblyName.FullName, assemblyName.Name, assemblyName.Version.ToString());
54+
yield return Tuples.assemblies(this, file, FullName, assemblyName.Name, assemblyName.Version.ToString());
5355

5456
if (cx.pdb != null)
5557
{

csharp/ql/src/semmle/code/cil/BasicBlock.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ private module Internal {
257257
or
258258
cfn.isJoin()
259259
or
260-
exists(ControlFlowNode pred | pred = cfn.getAPredecessor() |
261-
strictcount(pred.getASuccessor()) > 1
262-
)
260+
cfn.getAPredecessor().isBranch()
263261
}
264262

265263
/**

csharp/ql/src/semmle/code/cil/CIL.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ import Handler
1717
import ControlFlow
1818
import DataFlow
1919
import Attribute
20+
import Stubs
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Provides predicates for analysing the return values of callables.
3+
*/
4+
5+
private import CIL
6+
7+
cached
8+
private module Cached {
9+
/** Holds if method `m` always returns null. */
10+
cached
11+
predicate alwaysNullMethod(Method m) { forex(Expr e | m.canReturn(e) | alwaysNullExpr(e)) }
12+
13+
/** Holds if method `m` always returns non-null. */
14+
cached
15+
predicate alwaysNotNullMethod(Method m) { forex(Expr e | m.canReturn(e) | alwaysNotNullExpr(e)) }
16+
17+
/** Holds if method `m` always throws an exception. */
18+
cached
19+
predicate alwaysThrowsMethod(Method m) {
20+
m.hasBody() and
21+
not exists(m.getImplementation().getAnInstruction().(Return))
22+
}
23+
24+
/** Holds if method `m` always throws an exception of type `t`. */
25+
cached
26+
predicate alwaysThrowsException(Method m, Type t) {
27+
alwaysThrowsMethod(m) and
28+
forex(Throw ex | ex = m.getImplementation().getAnInstruction() | t = ex.getExpr().getType())
29+
}
30+
}
31+
import Cached
32+
33+
/** Holds if expression `expr` always evaluates to `null`. */
34+
private predicate alwaysNullExpr(Expr expr) {
35+
expr instanceof NullLiteral
36+
or
37+
alwaysNullMethod(expr.(StaticCall).getTarget())
38+
or
39+
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) |
40+
alwaysNullExpr(vu.getSource())
41+
)
42+
}
43+
44+
/** Holds if expression `expr` always evaluates to non-null. */
45+
private predicate alwaysNotNullExpr(Expr expr) {
46+
expr instanceof Opcodes::Newobj
47+
or
48+
expr instanceof Literal and not expr instanceof NullLiteral
49+
or
50+
alwaysNotNullMethod(expr.(StaticCall).getTarget())
51+
or
52+
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) |
53+
alwaysNotNullExpr(vu.getSource())
54+
)
55+
}

csharp/ql/src/semmle/code/cil/ControlFlow.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ class ControlFlowNode extends @cil_controlflow_node {
104104
Type getType() { none() }
105105

106106
/** Holds if this control flow node has more than one predecessor. */
107-
predicate isJoin() { count(getAPredecessor()) > 1 }
107+
predicate isJoin() { strictcount(this.getAPredecessor()) > 1 }
108+
109+
/** Holds if this control flow node has more than one successor. */
110+
predicate isBranch() { strictcount(this.getASuccessor()) > 1 }
108111
}
109112

110113
/**
@@ -137,6 +140,6 @@ class TrueFlow extends FlowType, TTrueFlow {
137140
}
138141

139142
/** False control flow. */
140-
class FalseFlow extends FlowType, TTrueFlow {
143+
class FalseFlow extends FlowType, TFalseFlow {
141144
override string toString() { result = "false" }
142145
}

csharp/ql/src/semmle/code/cil/DataFlow.qll

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Tainted extends TaintType, TTaintedValue { }
6060
private predicate localExactStep(DataFlowNode src, DataFlowNode sink) {
6161
src = sink.(Opcodes::Dup).getAnOperand()
6262
or
63-
DefUse::defUse(_, src, sink)
63+
defUse(_, src, sink)
6464
or
6565
src = sink.(ParameterReadAccess).getTarget()
6666
or
@@ -88,7 +88,7 @@ private predicate localTaintStep(DataFlowNode src, DataFlowNode sink) {
8888
}
8989

9090
cached
91-
private module DefUse {
91+
module DefUse {
9292
/**
9393
* A classification of variable references into reads and writes.
9494
*/
@@ -185,21 +185,26 @@ private module DefUse {
185185
)
186186
}
187187

188-
/** Holds if the update `def` can be used at the read `use`. */
188+
/** Holds if the variable update `vu` can be used at the read `use`. */
189189
cached
190-
predicate defUse(StackVariable target, DataFlowNode def, ReadAccess use) {
191-
exists(VariableUpdate vu | def = vu.getSource() |
192-
defReachesReadWithinBlock(target, vu, use)
193-
or
194-
exists(BasicBlock bb, int i |
195-
exists(refRank(bb, i, target, Read())) and
196-
use = bb.getNode(i) and
197-
defReachesEndOfBlock(bb.getAPredecessor(), vu, target) and
198-
not defReachesReadWithinBlock(target, _, use)
199-
)
190+
predicate variableUpdateUse(StackVariable target, VariableUpdate vu, ReadAccess use) {
191+
defReachesReadWithinBlock(target, vu, use)
192+
or
193+
exists(BasicBlock bb, int i |
194+
exists(refRank(bb, i, target, Read())) and
195+
use = bb.getNode(i) and
196+
defReachesEndOfBlock(bb.getAPredecessor(), vu, target) and
197+
not defReachesReadWithinBlock(target, _, use)
200198
)
201199
}
200+
201+
/** Holds if the update `def` can be used at the read `use`. */
202+
cached
203+
predicate defUse(StackVariable target, Expr def, ReadAccess use) {
204+
exists(VariableUpdate vu | def = vu.getSource() | variableUpdateUse(target, vu, use))
205+
}
202206
}
207+
private import DefUse
203208

204209
abstract library class VariableUpdate extends Instruction {
205210
abstract Expr getSource();

0 commit comments

Comments
 (0)