Skip to content

Commit aab9797

Browse files
author
Robert Marsh
committed
Merge branch 'main' into rdmarsh2/cpp/output-iterators-2
Resolve merge conflict in tests
2 parents 413c845 + 6218a48 commit aab9797

File tree

115 files changed

+8822
-436
lines changed

Some content is hidden

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

115 files changed

+8822
-436
lines changed

change-notes/1.26/analysis-javascript.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
| Client-side URL redirect (`js/client-side-unvalidated-url-redirection`) | More results | This query now recognizes some unsafe uses of `importScripts()` inside WebWorkers. |
5454
| Missing CSRF middleware (`js/missing-token-validation`) | More results | This query now recognizes writes to cookie and session variables as potentially vulnerable to CSRF attacks. |
5555
| Missing CSRF middleware (`js/missing-token-validation`) | Fewer results | This query now recognizes more ways of protecting against CSRF attacks. |
56+
| Client-side cross-site scripting (`js/xss`) | More results | This query now tracks data flow from `location.hash` more precisely. |
5657

5758

5859
## Changes to libraries
5960
* The predicate `TypeAnnotation.hasQualifiedName` now works in more cases when the imported library was not present during extraction.
61+
* The class `DomBasedXss::Configuration` has been deprecated, as it has been split into `DomBasedXss::HtmlInjectionConfiguration` and `DomBasedXss::JQueryHtmlOrSelectorInjectionConfiguration`. Unless specifically working with jQuery sinks, subclasses should instead be based on `HtmlInjectionConfiguration`. To use both configurations in a query, see [Xss.ql](https://github.com/github/codeql/blob/main/javascript/ql/src/Security/CWE-079/Xss.ql) for an example.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The `cpp/wrong-type-format-argument` and `cpp/non-portable-printf` queries have been hardened so that they do not produce nonsensical results on databases that contain errors (specifically the `ErroneousType`).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The 'Not enough memory allocated for pointer type' (cpp/allocation-too-small) and 'Not enough memory allocated for array of pointer type' (cpp/suspicious-allocation-size) queries have been improved. Previously some allocations would be reported by both queries, this no longer occurs. In addition more allocation functions are now understood by both queries.

cpp/ql/src/Critical/SizeCheck.ql

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,9 @@
1313
*/
1414

1515
import cpp
16+
import semmle.code.cpp.models.Models
1617

17-
class Allocation extends FunctionCall {
18-
Allocation() {
19-
exists(string name |
20-
this.getTarget().hasGlobalOrStdName(name) and
21-
(name = "malloc" or name = "calloc" or name = "realloc")
22-
)
23-
}
24-
25-
private string getName() { this.getTarget().hasGlobalOrStdName(result) }
26-
27-
int getSize() {
28-
this.getName() = "malloc" and
29-
this.getArgument(0).getValue().toInt() = result
30-
or
31-
this.getName() = "realloc" and
32-
this.getArgument(1).getValue().toInt() = result
33-
or
34-
this.getName() = "calloc" and
35-
result = this.getArgument(0).getValue().toInt() * this.getArgument(1).getValue().toInt()
36-
}
37-
}
38-
39-
predicate baseType(Allocation alloc, Type base) {
18+
predicate baseType(AllocationExpr alloc, Type base) {
4019
exists(PointerType pointer |
4120
pointer.getBaseType() = base and
4221
(
@@ -54,11 +33,12 @@ predicate decideOnSize(Type t, int size) {
5433
size = min(t.getSize())
5534
}
5635

57-
from Allocation alloc, Type base, int basesize, int allocated
36+
from AllocationExpr alloc, Type base, int basesize, int allocated
5837
where
5938
baseType(alloc, base) and
60-
allocated = alloc.getSize() and
39+
allocated = alloc.getSizeBytes() and
6140
decideOnSize(base, basesize) and
41+
alloc.(FunctionCall).getTarget() instanceof AllocationFunction and // exclude `new` and similar
6242
basesize > allocated
6343
select alloc,
6444
"Type '" + base.getName() + "' is " + basesize.toString() + " bytes, but only " +

cpp/ql/src/Critical/SizeCheck2.ql

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,9 @@
1313
*/
1414

1515
import cpp
16+
import semmle.code.cpp.models.Models
1617

17-
class Allocation extends FunctionCall {
18-
Allocation() { this.getTarget().hasGlobalOrStdName(["malloc", "calloc", "realloc"]) }
19-
20-
private string getName() { this.getTarget().hasGlobalOrStdName(result) }
21-
22-
int getSize() {
23-
this.getName() = "malloc" and
24-
this.getArgument(0).getValue().toInt() = result
25-
or
26-
this.getName() = "realloc" and
27-
this.getArgument(1).getValue().toInt() = result
28-
or
29-
this.getName() = "calloc" and
30-
result = this.getArgument(0).getValue().toInt() * this.getArgument(1).getValue().toInt()
31-
}
32-
}
33-
34-
predicate baseType(Allocation alloc, Type base) {
18+
predicate baseType(AllocationExpr alloc, Type base) {
3519
exists(PointerType pointer |
3620
pointer.getBaseType() = base and
3721
(
@@ -44,16 +28,23 @@ predicate baseType(Allocation alloc, Type base) {
4428
)
4529
}
4630

47-
from Allocation alloc, Type base, int basesize, int allocated
31+
predicate decideOnSize(Type t, int size) {
32+
// If the codebase has more than one type with the same name, it can have more than one size.
33+
size = min(t.getSize())
34+
}
35+
36+
from AllocationExpr alloc, Type base, int basesize, int allocated
4837
where
4938
baseType(alloc, base) and
50-
allocated = alloc.getSize() and
39+
allocated = alloc.getSizeBytes() and
40+
decideOnSize(base, basesize) and
41+
alloc.(FunctionCall).getTarget() instanceof AllocationFunction and // exclude `new` and similar
5142
// If the codebase has more than one type with the same name, check if any matches
5243
not exists(int size | base.getSize() = size |
5344
size = 0 or
5445
(allocated / size) * size = allocated
5546
) and
56-
basesize = min(base.getSize())
47+
not basesize > allocated // covered by SizeCheck.ql
5748
select alloc,
5849
"Allocated memory (" + allocated.toString() + " bytes) is not a multiple of the size of '" +
5950
base.getName() + "' (" + basesize.toString() + " bytes)."

cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ where
155155
not actual.getUnspecifiedType().(IntegralType).getSize() = sizeof_IntType()
156156
) and
157157
not arg.isAffectedByMacro() and
158-
not arg.isFromUninstantiatedTemplate(_)
158+
not arg.isFromUninstantiatedTemplate(_) and
159+
not actual.getUnspecifiedType() instanceof ErroneousType
159160
select arg,
160161
"This argument should be of type '" + expected.getName() + "' but is of type '" +
161162
actual.getUnspecifiedType().getName() + "'"

cpp/ql/src/Likely Bugs/Memory Management/Padding/NonPortablePrintf.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ where
8888
not arg.isAffectedByMacro() and
8989
size32 = ilp32.paddedSize(actual) and
9090
size64 = lp64.paddedSize(actual) and
91-
size64 != size32
91+
size64 != size32 and
92+
not actual instanceof ErroneousType
9293
select arg,
9394
"This argument should be of type '" + expected.getName() + "' but is of type '" + actual.getName()
9495
+ "' (which changes size from " + size32 + " to " + size64 + " on 64-bit systems)."

cpp/ql/src/semmle/code/cpp/Print.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import cpp
2+
private import PrintAST
3+
4+
/**
5+
* Print function declarations only if there is a `PrintASTConfiguration`
6+
* that requests that function, or no `PrintASTConfiguration` exists.
7+
*/
8+
private predicate shouldPrintDeclaration(Declaration decl) {
9+
not decl instanceof Function
10+
or
11+
not exists(PrintASTConfiguration c)
12+
or
13+
exists(PrintASTConfiguration config | config.shouldPrintFunction(decl))
14+
}
215

316
/**
417
* Gets a string containing the scope in which this declaration is declared.
@@ -48,6 +61,8 @@ private string getTemplateArgumentString(Declaration d, int i) {
4861
* A `Declaration` extended to add methods for generating strings useful only for dumps and debugging.
4962
*/
5063
abstract private class DumpDeclaration extends Declaration {
64+
DumpDeclaration() { shouldPrintDeclaration(this) }
65+
5166
/**
5267
* Gets a string that uniquely identifies this declaration, suitable for use when debugging queries. Only holds for
5368
* functions, user-defined types, global and namespace-scope variables, and member variables.

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7208,3 +7208,52 @@
72087208
| vector.cpp:449:6:449:7 | it | vector.cpp:449:4:449:4 | call to operator++ | |
72097209
| vector.cpp:449:11:449:16 | call to source | vector.cpp:449:3:449:3 | ref arg call to operator* | TAINT |
72107210
| vector.cpp:450:8:450:10 | ref arg out | vector.cpp:451:2:451:2 | out | |
7211+
| vector.cpp:467:22:467:25 | call to vector | vector.cpp:471:8:471:8 | v | |
7212+
| vector.cpp:467:22:467:25 | call to vector | vector.cpp:472:11:472:11 | v | |
7213+
| vector.cpp:467:22:467:25 | call to vector | vector.cpp:473:8:473:8 | v | |
7214+
| vector.cpp:467:22:467:25 | call to vector | vector.cpp:474:2:474:2 | v | |
7215+
| vector.cpp:468:11:468:16 | call to source | vector.cpp:472:18:472:18 | s | |
7216+
| vector.cpp:469:10:469:11 | 0 | vector.cpp:472:13:472:13 | i | |
7217+
| vector.cpp:471:8:471:8 | ref arg v | vector.cpp:472:11:472:11 | v | |
7218+
| vector.cpp:471:8:471:8 | ref arg v | vector.cpp:473:8:473:8 | v | |
7219+
| vector.cpp:471:8:471:8 | ref arg v | vector.cpp:474:2:474:2 | v | |
7220+
| vector.cpp:472:10:472:14 | & ... | vector.cpp:472:3:472:8 | call to memcpy | |
7221+
| vector.cpp:472:10:472:14 | ref arg & ... | vector.cpp:472:12:472:12 | call to operator[] [inner post update] | |
7222+
| vector.cpp:472:11:472:11 | ref arg v | vector.cpp:473:8:473:8 | v | |
7223+
| vector.cpp:472:11:472:11 | ref arg v | vector.cpp:474:2:474:2 | v | |
7224+
| vector.cpp:472:11:472:11 | v | vector.cpp:472:12:472:12 | call to operator[] | TAINT |
7225+
| vector.cpp:472:12:472:12 | call to operator[] | vector.cpp:472:10:472:14 | & ... | |
7226+
| vector.cpp:472:12:472:12 | call to operator[] [inner post update] | vector.cpp:472:11:472:11 | ref arg v | TAINT |
7227+
| vector.cpp:472:17:472:18 | & ... | vector.cpp:472:3:472:8 | call to memcpy | TAINT |
7228+
| vector.cpp:472:17:472:18 | & ... | vector.cpp:472:10:472:14 | ref arg & ... | TAINT |
7229+
| vector.cpp:472:18:472:18 | s | vector.cpp:472:10:472:14 | ref arg & ... | |
7230+
| vector.cpp:472:18:472:18 | s | vector.cpp:472:17:472:18 | & ... | |
7231+
| vector.cpp:473:8:473:8 | ref arg v | vector.cpp:474:2:474:2 | v | |
7232+
| vector.cpp:477:24:477:27 | call to vector | vector.cpp:483:8:483:9 | cs | |
7233+
| vector.cpp:477:24:477:27 | call to vector | vector.cpp:484:11:484:12 | cs | |
7234+
| vector.cpp:477:24:477:27 | call to vector | vector.cpp:486:8:486:9 | cs | |
7235+
| vector.cpp:477:24:477:27 | call to vector | vector.cpp:487:2:487:2 | cs | |
7236+
| vector.cpp:478:21:478:37 | call to source | vector.cpp:480:22:480:24 | src | |
7237+
| vector.cpp:478:21:478:37 | call to source | vector.cpp:482:8:482:10 | src | |
7238+
| vector.cpp:478:21:478:37 | call to source | vector.cpp:484:25:484:27 | src | |
7239+
| vector.cpp:478:21:478:37 | call to source | vector.cpp:485:8:485:10 | src | |
7240+
| vector.cpp:479:23:479:24 | 10 | vector.cpp:484:14:484:17 | offs | |
7241+
| vector.cpp:480:26:480:31 | call to length | vector.cpp:484:38:484:40 | len | |
7242+
| vector.cpp:482:8:482:10 | ref arg src | vector.cpp:484:25:484:27 | src | |
7243+
| vector.cpp:482:8:482:10 | ref arg src | vector.cpp:485:8:485:10 | src | |
7244+
| vector.cpp:483:8:483:9 | ref arg cs | vector.cpp:484:11:484:12 | cs | |
7245+
| vector.cpp:483:8:483:9 | ref arg cs | vector.cpp:486:8:486:9 | cs | |
7246+
| vector.cpp:483:8:483:9 | ref arg cs | vector.cpp:487:2:487:2 | cs | |
7247+
| vector.cpp:484:10:484:22 | & ... | vector.cpp:484:3:484:8 | call to memcpy | |
7248+
| vector.cpp:484:10:484:22 | ref arg & ... | vector.cpp:484:13:484:13 | call to operator[] [inner post update] | |
7249+
| vector.cpp:484:11:484:12 | cs | vector.cpp:484:13:484:13 | call to operator[] | TAINT |
7250+
| vector.cpp:484:11:484:12 | ref arg cs | vector.cpp:486:8:486:9 | cs | |
7251+
| vector.cpp:484:11:484:12 | ref arg cs | vector.cpp:487:2:487:2 | cs | |
7252+
| vector.cpp:484:13:484:13 | call to operator[] | vector.cpp:484:10:484:22 | & ... | |
7253+
| vector.cpp:484:13:484:13 | call to operator[] [inner post update] | vector.cpp:484:11:484:12 | ref arg cs | TAINT |
7254+
| vector.cpp:484:14:484:17 | offs | vector.cpp:484:14:484:21 | ... + ... | TAINT |
7255+
| vector.cpp:484:21:484:21 | 1 | vector.cpp:484:14:484:21 | ... + ... | TAINT |
7256+
| vector.cpp:484:25:484:27 | src | vector.cpp:484:29:484:33 | call to c_str | TAINT |
7257+
| vector.cpp:484:29:484:33 | call to c_str | vector.cpp:484:3:484:8 | call to memcpy | TAINT |
7258+
| vector.cpp:484:29:484:33 | call to c_str | vector.cpp:484:10:484:22 | ref arg & ... | TAINT |
7259+
| vector.cpp:486:8:486:9 | ref arg cs | vector.cpp:487:2:487:2 | cs | |

cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void *memcpy(void *dest, void *src, int len);
192192
void test_memcpy(int *source) {
193193
int x;
194194
memcpy(&x, source, sizeof(int));
195-
sink(x);
195+
sink(x); // tainted
196196
}
197197

198198
// --- std::swap ---

0 commit comments

Comments
 (0)