Skip to content

Commit 2c96e6c

Browse files
committed
Merge remote-tracking branch 'upstream/main' into main
2 parents 92c00cb + 1c2b9f9 commit 2c96e6c

File tree

928 files changed

+92483
-12195
lines changed

Some content is hidden

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

928 files changed

+92483
-12195
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ name: "Code scanning - action"
22

33
on:
44
push:
5+
branches:
6+
- main
7+
- 'rc/*'
58
pull_request:
9+
branches:
10+
- main
11+
- 'rc/*'
612
schedule:
713
- cron: '0 9 * * 1'
814

@@ -14,16 +20,7 @@ jobs:
1420
steps:
1521
- name: Checkout repository
1622
uses: actions/checkout@v2
17-
with:
18-
# We must fetch at least the immediate parents so that if this is
19-
# a pull request then we can checkout the head.
20-
fetch-depth: 2
21-
22-
# If this run was triggered by a pull request event, then checkout
23-
# the head of the pull request instead of the merge commit.
24-
- run: git checkout HEAD^2
25-
if: ${{ github.event_name == 'pull_request' }}
26-
23+
2724
# Initializes the CodeQL tools for scanning.
2825
- name: Initialize CodeQL
2926
uses: github/codeql-action/init@v1

.github/workflows/generate-query-help-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
uses: ammaraskar/sphinx-action@8b4f60114d7fd1faeba1a712269168508d4750d2 # v0.4
5151
with:
5252
docs-folder: "query-help/"
53-
pre-build-command: "python -m pip install --upgrade recommonmark"
53+
pre-build-command: "python -m pip install --upgrade recommonmark && python -m pip install --upgrade sphinx-markdown-tables"
5454
build-command: "sphinx-build -b dirhtml . _build"
5555
- name: Upload HTML artifacts
5656
uses: actions/upload-artifact@v2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# Byte-compiled python files
1818
*.pyc
1919

20+
# python virtual environment folder
21+
.venv/
22+
2023
# It's useful (though not required) to be able to unpack codeql in the ql checkout itself
2124
/codeql/
2225

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm
2+
* A new query (`cpp/unsigned-difference-expression-compared-zero`) is run but not yet displayed on LGTM. The query finds unsigned subtractions used in relational comparisons with the value 0. This query was originally submitted as an experimental query by @ihsinme in https://github.com/github/codeql/pull/4745.

cpp/config/suites/cpp/correctness

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/MistypedFunctionArguments.ql: /Correctness/Dangerous Conversions
1111
+ semmlecode-cpp-queries/Security/CWE/CWE-253/HResultBooleanConversion.ql: /Correctness/Dangerous Conversions
1212
+ semmlecode-cpp-queries/Likely Bugs/OO/UnsafeUseOfThis.ql: /Correctness/Dangerous Conversions
13+
+ semmlecode-cpp-queries/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero.ql: /Correctness/Dangerous Conversions
1314
# Consistent Use
1415
+ semmlecode-cpp-queries/Critical/ReturnValueIgnored.ql: /Correctness/Consistent Use
1516
+ semmlecode-cpp-queries/Likely Bugs/InconsistentCheckReturnNull.ql: /Correctness/Consistent Use

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class UntrustedDataToExternalAPIConfig extends TaintTracking::Configuration {
4646
UntrustedDataToExternalAPIConfig() { this = "UntrustedDataToExternalAPIConfig" }
4747

4848
override predicate isSource(DataFlow::Node source) {
49-
exists(RemoteFlowFunction remoteFlow |
49+
exists(RemoteFlowSourceFunction remoteFlow |
5050
remoteFlow = source.asExpr().(Call).getTarget() and
5151
remoteFlow.hasRemoteFlowSource(_, _)
5252
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
unsigned limit = get_limit();
2+
unsigned total = 0;
3+
while (limit - total > 0) { // wrong: if `total` is greater than `limit` this will underflow and continue executing the loop.
4+
total += get_data();
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
This rule finds relational comparisons between the result of an unsigned subtraction and the value <code>0</code>.
8+
Such comparisons are likely to be wrong as the value of an unsigned subtraction can never be negative. So the
9+
relational comparison ends up checking whether the result of the subtraction is equal to <code>0</code>.
10+
This is probably not what the programmer intended.
11+
</p>
12+
</overview>
13+
<recommendation>
14+
15+
<p>If a relational comparison is intended, consider casting the result of the subtraction to a signed type.
16+
If the intention was to test for equality, consider replacing the relational comparison with an equality test.
17+
</p>
18+
19+
</recommendation>
20+
<example>
21+
<sample src="UnsignedDifferenceExpressionComparedZero.c" />
22+
23+
</example>
24+
<references>
25+
26+
<li>SEI CERT C Coding Standard:
27+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules">INT02-C. Understand integer conversion rules</a>.
28+
</li>
29+
30+
</references>
31+
</qhelp>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @name Unsigned difference expression compared to zero
3+
* @description A subtraction with an unsigned result can never be negative. Using such an expression in a relational comparison with `0` is likely to be wrong.
4+
* @kind problem
5+
* @id cpp/unsigned-difference-expression-compared-zero
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags security
9+
* correctness
10+
* external/cwe/cwe-191
11+
*/
12+
13+
import cpp
14+
import semmle.code.cpp.commons.Exclusions
15+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
16+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
import semmle.code.cpp.controlflow.Guards
18+
19+
/** Holds if `sub` is guarded by a condition which ensures that `left >= right`. */
20+
pragma[noinline]
21+
predicate isGuarded(SubExpr sub, Expr left, Expr right) {
22+
exists(GuardCondition guard |
23+
guard.controls(sub.getBasicBlock(), true) and
24+
guard.ensuresLt(left, right, 0, sub.getBasicBlock(), false)
25+
)
26+
}
27+
28+
/** Holds if `sub` will never be negative. */
29+
predicate nonNegative(SubExpr sub) {
30+
not exprMightOverflowNegatively(sub.getFullyConverted())
31+
or
32+
// The subtraction is guarded by a check of the form `left >= right`.
33+
exists(GVN left, GVN right |
34+
// This is basically a poor man's version of a directional unbind operator.
35+
strictcount([left, globalValueNumber(sub.getLeftOperand())]) = 1 and
36+
strictcount([right, globalValueNumber(sub.getRightOperand())]) = 1 and
37+
isGuarded(sub, left.getAnExpr(), right.getAnExpr())
38+
)
39+
}
40+
41+
from RelationalOperation ro, SubExpr sub
42+
where
43+
not isFromMacroDefinition(ro) and
44+
not isFromMacroDefinition(sub) and
45+
ro.getLesserOperand().getValue().toInt() = 0 and
46+
ro.getGreaterOperand() = sub and
47+
sub.getFullyConverted().getUnspecifiedType().(IntegralType).isUnsigned() and
48+
not nonNegative(sub)
49+
select ro, "Unsigned subtraction can never be negative."

cpp/ql/src/Security/CWE/CWE-457/InitializationFunctions.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ class InitializationFunction extends Function {
353353
// Destination range is zeroed out on failure, assuming first two parameters are valid
354354
"memcpy_s",
355355
// This zeroes the memory unconditionally
356-
"SeCreateAccessState"
356+
"SeCreateAccessState",
357+
// Argument initialization is optional, but always succeeds
358+
"KeGetCurrentProcessorNumberEx"
357359
]
358360
)
359361
}

0 commit comments

Comments
 (0)