Skip to content

Commit 1b81526

Browse files
committed
Merge remote-tracking branch 'upstream/master' into exceptionXss
2 parents 525da97 + 351cb46 commit 1b81526

File tree

259 files changed

+12994
-8194
lines changed

Some content is hidden

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

259 files changed

+12994
-8194
lines changed

CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
/cpp/ @Semmle/cpp-analysis
12
/csharp/ @Semmle/cs
23
/java/ @Semmle/java
34
/javascript/ @Semmle/js
4-
/cpp/ @Semmle/cpp-analysis
5+
/python/ @Semmle/python
56
/cpp/**/*.qhelp @hubwriter
67
/csharp/**/*.qhelp @jf205
78
/java/**/*.qhelp @felicitymay

change-notes/1.23/analysis-cpp.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The following changes in version 1.23 affect C/C++ analysis in all applications.
99
| **Query** | **Tags** | **Purpose** |
1010
|-----------------------------|-----------|--------------------------------------------------------------------|
1111
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | reliability, japanese-era | This query is a combination of two old queries that were identical in purpose but separate as an implementation detail. This new query replaces Hard-coded Japanese era start date in call (`cpp/japanese-era/constructor-or-method-with-exact-era-date`) and Hard-coded Japanese era start date in struct (`cpp/japanese-era/struct-with-exact-era-date`). |
12-
| Signed overflow check (`cpp/signed-overflow-check`) | correctness, reliability | Finds overflow checks that rely on signed integer addition to overflow, which has undefined behavior. Example: `a + b < a`. |
12+
| Signed overflow check (`cpp/signed-overflow-check`) | correctness, security | Finds overflow checks that rely on signed integer addition to overflow, which has undefined behavior. Example: `a + b < a`. |
13+
| Pointer overflow check (`cpp/pointer-overflow-check`) | correctness, security | Finds overflow checks that rely on pointer addition to overflow, which has undefined behavior. Example: `ptr + a < ptr`. |
1314

1415
## Changes to existing queries
1516

change-notes/1.23/analysis-javascript.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88

99
* Support for the following frameworks and libraries has been improved:
1010
- [firebase](https://www.npmjs.com/package/firebase)
11+
- [get-them-args](https://www.npmjs.com/package/get-them-args)
12+
- [minimist](https://www.npmjs.com/package/minimist)
1113
- [mongodb](https://www.npmjs.com/package/mongodb)
1214
- [mongoose](https://www.npmjs.com/package/mongoose)
15+
- [optimist](https://www.npmjs.com/package/optimist)
16+
- [parse-torrent](https://www.npmjs.com/package/parse-torrent)
1317
- [rate-limiter-flexible](https://www.npmjs.com/package/rate-limiter-flexible)
18+
- [yargs](https://www.npmjs.com/package/yargs)
1419

1520
* The call graph has been improved to resolve method calls in more cases. This may produce more security alerts.
1621

@@ -20,14 +25,15 @@
2025

2126
| **Query** | **Tags** | **Purpose** |
2227
|---------------------------------------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
23-
| Unused index variable (`js/unused-index-variable`) | correctness | Highlights loops that iterate over an array, but do not use the index variable to access array elements, indicating a possible typo or logic error. Results are shown on LGTM by default. |
28+
| Ignoring result from pure array method (`js/ignore-array-result`) | maintainability, correctness | Highlights calls to array methods without side effects where the return value is ignored. Results are shown on LGTM by default. |
29+
| Incomplete URL scheme check (`js/incomplete-url-scheme-check`) | security, correctness, external/cwe/cwe-020 | Highlights checks for `javascript:` URLs that do not take `data:` or `vbscript:` URLs into account. Results are shown on LGTM by default. |
2430
| Loop bound injection (`js/loop-bound-injection`) | security, external/cwe/cwe-834 | Highlights loops where a user-controlled object with an arbitrary .length value can trick the server to loop indefinitely. Results are shown on LGTM by default. |
25-
| Suspicious method name (`js/suspicious-method-name-declaration`) | correctness, typescript, methods | Highlights suspiciously named methods where the developer likely meant to write a constructor or function. Results are shown on LGTM by default. |
2631
| Shell command built from environment values (`js/shell-command-injection-from-environment`) | correctness, security, external/cwe/cwe-078, external/cwe/cwe-088 | Highlights shell commands that may change behavior inadvertently depending on the execution environment, indicating a possible violation of [CWE-78](https://cwe.mitre.org/data/definitions/78.html). Results are shown on LGTM by default.|
32+
| Suspicious method name (`js/suspicious-method-name-declaration`) | correctness, typescript, methods | Highlights suspiciously named methods where the developer likely meant to write a constructor or function. Results are shown on LGTM by default. |
33+
| Unreachable method overloads (`js/unreachable-method-overloads`) | correctness, typescript | Highlights method overloads that are impossible to use from client code. Results are shown on LGTM by default. |
34+
| Unused index variable (`js/unused-index-variable`) | correctness | Highlights loops that iterate over an array, but do not use the index variable to access array elements, indicating a possible typo or logic error. Results are shown on LGTM by default. |
2735
| Use of returnless function (`js/use-of-returnless-function`) | maintainability, correctness | Highlights calls where the return value is used, but the callee never returns a value. Results are shown on LGTM by default. |
2836
| Useless regular expression character escape (`js/useless-regexp-character-escape`) | correctness, security, external/cwe/cwe-20 | Highlights regular expression strings with useless character escapes, indicating a possible violation of [CWE-20](https://cwe.mitre.org/data/definitions/20.html). Results are shown on LGTM by default. |
29-
| Unreachable method overloads (`js/unreachable-method-overloads`) | correctness, typescript | Highlights method overloads that are impossible to use from client code. Results are shown on LGTM by default. |
30-
| Ignoring result from pure array method (`js/ignore-array-result`) | maintainability, correctness | Highlights calls to array methods without side effects where the return value is ignored. Results are shown on LGTM by default. |
3137

3238
## Changes to existing queries
3339

@@ -51,6 +57,11 @@
5157
## Changes to libraries
5258

5359
* `Expr.getDocumentation()` now handles chain assignments.
60+
* String literals are now parsed as regular expressions.
61+
Consequently, a `RegExpTerm` may occur as part of a string literal or
62+
as a regular expression literal. Queries that search for regular expressions may need to
63+
use `RegExpTerm.isPartOfRegExpLiteral` or `RegExpTerm.isUsedAsRegExp` to restrict the search.
64+
A regular expression AST can be obtained from a string literal using `StringLiteral.asRegExp`.
5465

5566
## Removal of deprecated queries
5667

cpp/ql/src/Likely Bugs/Arithmetic/PointlessSelfComparison.ql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313

1414
import cpp
1515
import PointlessSelfComparison
16+
import semmle.code.cpp.commons.Exclusions
1617

1718
from ComparisonOperation cmp
1819
where
1920
pointlessSelfComparison(cmp) and
2021
not nanTest(cmp) and
2122
not overflowTest(cmp) and
2223
not cmp.isFromTemplateInstantiation(_) and
23-
not exists(MacroInvocation mi |
24-
// cmp is in mi
25-
mi.getAnExpandedElement() = cmp and
26-
// and cmp was apparently not passed in as a macro parameter
27-
cmp.getLocation().getStartLine() = mi.getLocation().getStartLine() and
28-
cmp.getLocation().getStartColumn() = mi.getLocation().getStartColumn()
29-
)
24+
not isFromMacroDefinition(cmp)
3025
select cmp, "Self comparison."

cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @name Undefined result of signed test for overflow
2+
* @name Signed overflow check
33
* @description Testing for overflow by adding a value to a variable
44
* to see if it "wraps around" works only for
55
* unsigned integer values.
66
* @kind problem
77
* @problem.severity warning
88
* @precision high
99
* @id cpp/signed-overflow-check
10-
* @tags reliability
10+
* @tags correctness
1111
* security
1212
*/
1313

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2+
return ptr + a >= ptr_end || ptr + a < ptr; // BAD
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2+
return a >= ptr_end - ptr; // GOOD
3+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
The expression <code>ptr + a &lt; ptr</code> is equivalent to <code>a &lt;
8+
0</code>, and an optimizing compiler is likely to make that replacement,
9+
thereby removing a range check that might have been necessary for security.
10+
If <code>a</code> is known to be non-negative, the compiler can even replace <code>ptr +
11+
a &lt; ptr</code> with <code>false</code>.
12+
</p>
13+
14+
<p>
15+
The reason is that pointer arithmetic overflow in C/C++ is undefined
16+
behavior. The optimizing compiler can assume that the program has no
17+
undefined behavior, which means that adding a positive number to <code>ptr</code> cannot
18+
produce a pointer less than <code>ptr</code>.
19+
</p>
20+
</overview>
21+
<recommendation>
22+
<p>
23+
To check whether an index <code>a</code> is less than the length of an array,
24+
simply compare these two numbers as unsigned integers: <code>a &lt; ARRAY_LENGTH</code>.
25+
If the length of the array is defined as the difference between two pointers
26+
<code>ptr</code> and <code>p_end</code>, write <code>a &lt; p_end - ptr</code>.
27+
If a is <code>signed</code>, cast it to <code>unsigned</code>
28+
in order to guard against negative <code>a</code>. For example, write
29+
<code>(size_t)a &lt; p_end - ptr</code>.
30+
</p>
31+
</recommendation>
32+
<example>
33+
<p>
34+
An invalid check for pointer overflow is most often seen as part of checking
35+
whether a number <code>a</code> is too large by checking first if adding the
36+
number to <code>ptr</code> goes past the end of an allocation and then
37+
checking if adding it to <code>ptr</code> creates a pointer so large that it
38+
overflows and wraps around.
39+
</p>
40+
41+
<sample src="PointerOverflow-bad.cpp" />
42+
43+
<p>
44+
In both of these checks, the operations are performed in the wrong order.
45+
First, an expression that may cause undefined behavior is evaluated
46+
(<code>ptr + a</code>), and then the result is checked for being in range.
47+
But once undefined behavior has happened in the pointer addition, it cannot
48+
be recovered from: it's too late to perform the range check after a possible
49+
pointer overflow.
50+
</p>
51+
52+
<p>
53+
While it's not the subject of this query, the expression <code>ptr + a &lt;
54+
ptr_end</code> is also an invalid range check. It's undefined behavor in
55+
C/C++ to create a pointer that points more than one past the end of an
56+
allocation.
57+
</p>
58+
59+
<p>
60+
The next example shows how to portably check whether an unsigned number is outside the
61+
range of an allocation between <code>ptr</code> and <code>ptr_end</code>.
62+
</p>
63+
<sample src="PointerOverflow-good.cpp" />
64+
</example>
65+
<references>
66+
<li>Embedded in Academia: <a href="https://blog.regehr.org/archives/1395">Pointer Overflow Checking</a>.</li>
67+
<li>LWN: <a href="https://lwn.net/Articles/278137/">GCC and pointer overflows</a>.</li>
68+
</references>
69+
</qhelp>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @name Pointer overflow check
3+
* @description Adding a value to a pointer to check if it overflows relies
4+
* on undefined behavior and may lead to memory corruption.
5+
* @kind problem
6+
* @problem.severity error
7+
* @precision high
8+
* @id cpp/pointer-overflow-check
9+
* @tags reliability
10+
* security
11+
*/
12+
13+
import cpp
14+
private import semmle.code.cpp.valuenumbering.GlobalValueNumbering
15+
private import semmle.code.cpp.commons.Exclusions
16+
17+
from RelationalOperation ro, PointerAddExpr add, Expr expr1, Expr expr2
18+
where
19+
ro.getAnOperand() = add and
20+
add.getAnOperand() = expr1 and
21+
ro.getAnOperand() = expr2 and
22+
globalValueNumber(expr1) = globalValueNumber(expr2) and
23+
// Exclude macros but not their arguments
24+
not isFromMacroDefinition(ro) and
25+
// There must be a compilation of this file without a flag that makes pointer
26+
// overflow well defined.
27+
exists(Compilation c | c.getAFileCompiled() = ro.getFile() |
28+
not c.getAnArgument() = "-fwrapv-pointer" and
29+
not c.getAnArgument() = "-fno-strict-overflow"
30+
)
31+
select ro, "Range check relying on pointer overflow."

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ abstract class Declaration extends Locatable, @declaration {
223223
* Will have `getTemplateArgument(0)` return `T`, and
224224
* `getTemplateArgument(1)` return `X`.
225225
*
226-
* `Foo<int, 1> bar;
226+
* `Foo<int, 1> bar;`
227227
*
228228
* Will have `getTemplateArgument())` return `int`, and
229229
* `getTemplateArgument(1)` return `1`.

0 commit comments

Comments
 (0)