Skip to content

Commit fbb5d71

Browse files
committed
Merge branch 'main' into post-release-prep/codeql-cli-2.7.5
2 parents 8a2d92b + bba8e45 commit fbb5d71

File tree

744 files changed

+39369
-19095
lines changed

Some content is hidden

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

744 files changed

+39369
-19095
lines changed

.codeqlmanifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"*/ql/lib/qlpack.yml",
55
"*/ql/test/qlpack.yml",
66
"*/ql/examples/qlpack.yml",
7-
"*/upgrades/qlpack.yml",
87
"cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/qlpack.yml",
98
"javascript/ql/experimental/adaptivethreatmodeling/lib/qlpack.yml",
109
"javascript/ql/experimental/adaptivethreatmodeling/src/qlpack.yml",
10+
"csharp/ql/campaigns/Solorigate/lib/qlpack.yml",
11+
"csharp/ql/campaigns/Solorigate/src/qlpack.yml",
12+
"csharp/ql/campaigns/Solorigate/test/qlpack.yml",
1113
"misc/legacy-support/*/qlpack.yml",
1214
"misc/suite-helpers/qlpack.yml",
1315
"ruby/extractor-pack/codeql-extractor.yml",

CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
/docs/query-*-style-guide.md @github/codeql-analysis-reviewers
2828

2929
# QL for QL reviewers
30-
/ql/ @erik-krogh @tausbn
30+
/ql/ @github/codeql-ql-for-ql-reviewers

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# CodeQL
22

3-
This open source repository contains the standard CodeQL libraries and queries that power [LGTM](https://lgtm.com) and the other CodeQL products that [GitHub](https://github.com) makes available to its customers worldwide. For the queries, libraries, and extractor that power Go analysis, visit the [CodeQL for Go repository](https://github.com/github/codeql-go).
3+
This open source repository contains the standard CodeQL libraries and queries that power [GitHub Advanced Security](https://github.com/features/security/code) and the other application security products that [GitHub](https://github.com/features/security/) makes available to its customers worldwide. For the queries, libraries, and extractor that power Go analysis, visit the [CodeQL for Go repository](https://github.com/github/codeql-go).
44

55
## How do I learn CodeQL and run queries?
66

77
There is [extensive documentation](https://codeql.github.com/docs/) on getting started with writing CodeQL.
8-
You can use the [interactive query console](https://lgtm.com/help/lgtm/using-query-console) on LGTM.com or the [CodeQL for Visual Studio Code](https://codeql.github.com/docs/codeql-for-visual-studio-code/) extension to try out your queries on any open source project that's currently being analyzed.
8+
You can use the [CodeQL for Visual Studio Code](https://codeql.github.com/docs/codeql-for-visual-studio-code/) extension or the [interactive query console](https://lgtm.com/help/lgtm/using-query-console) on LGTM.com (Semmle Legacy product) to try out your queries on any open source project that's currently being analyzed.
99

1010
## Contributing
1111

1212
We welcome contributions to our standard library and standard checks. Do you have an idea for a new check, or how to improve an existing query? Then please go ahead and open a pull request! Before you do, though, please take the time to read our [contributing guidelines](CONTRIBUTING.md). You can also consult our [style guides](https://github.com/github/codeql/tree/main/docs) to learn how to format your code for consistency and clarity, how to write query metadata, and how to write query help documentation for your query.
1313

1414
## License
1515

16-
The code in this repository is licensed under the [MIT License](LICENSE) by [GitHub](https://github.com).
16+
The code in this repository is licensed under the [MIT License](LICENSE) by [GitHub](https://github.com). The use of CodeQL on open source code is licensed under specific [Terms & Conditions](https://securitylab.github.com/tools/codeql/license/) UNLESS you have a commercial license in place. If you'd like to use CodeQL with a commercial codebase, please [contact us](https://github.com/enterprise/contact) for further help.
1717

1818
## Visual Studio Code integration
1919

cpp/config/suites/security/cwe-120

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
@name Badly bounded write (CWE-120)
66
+ semmlecode-cpp-queries/Security/CWE/CWE-120/OverrunWrite.ql: /CWE/CWE-120
77
@name Potentially overrunning write (CWE-120)
8+
+ semmlecode-cpp-queries/Security/CWE/CWE-120/VeryLikelyOverrunWrite.ql: /CWE/CWE-120
9+
@name Likely overrunning write
810
+ semmlecode-cpp-queries/Security/CWE/CWE-120/OverrunWriteFloat.ql: /CWE/CWE-120
911
@name Potentially overrunning write with float to string conversion (CWE-120)
1012
+ semmlecode-cpp-queries/Best Practices/Likely Errors/OffsetUseBeforeRangeCheck.ql: /CWE/CWE-120
1113
@name Array offset used before range check (CWE-120)
1214
+ semmlecode-cpp-queries/Likely Bugs/Memory Management/UnsafeUseOfStrcat.ql: /CWE/CWE-120
13-
@name Potentially unsafe use of strcat (CWE-120)
15+
@name Potentially unsafe use of strcat (CWE-120)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: deprecated
3+
---
4+
* The `codeql/cpp-upgrades` CodeQL pack has been removed. All upgrades scripts have been merged into the `codeql/cpp-all` CodeQL pack.

cpp/ql/lib/qlpack.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ groups: cpp
44
dbscheme: semmlecode.cpp.dbscheme
55
extractor: cpp
66
library: true
7-
dependencies:
8-
codeql/cpp-upgrades: ^0.0.3
7+
upgrades: upgrades

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ private import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1010
private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1111

1212
private newtype TBufferWriteEstimationReason =
13-
TNoSpecifiedEstimateReason() or
13+
TUnspecifiedEstimateReason() or
1414
TTypeBoundsAnalysis() or
15+
TWidenedValueFlowAnalysis() or
1516
TValueFlowAnalysis()
1617

18+
private predicate gradeToReason(int grade, TBufferWriteEstimationReason reason) {
19+
// when combining reasons, lower grade takes precedence
20+
grade = 0 and reason = TUnspecifiedEstimateReason()
21+
or
22+
grade = 1 and reason = TTypeBoundsAnalysis()
23+
or
24+
grade = 2 and reason = TWidenedValueFlowAnalysis()
25+
or
26+
grade = 3 and reason = TValueFlowAnalysis()
27+
}
28+
1729
/**
1830
* A reason for a specific buffer write size estimate.
1931
*/
@@ -32,24 +44,24 @@ abstract class BufferWriteEstimationReason extends TBufferWriteEstimationReason
3244
* Combine estimate reasons. Used to give a reason for the size of a format string
3345
* conversion given reasons coming from its individual specifiers.
3446
*/
35-
abstract BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other);
47+
BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other) {
48+
exists(int grade, int otherGrade |
49+
gradeToReason(grade, this) and gradeToReason(otherGrade, other)
50+
|
51+
if otherGrade < grade then result = other else result = this
52+
)
53+
}
3654
}
3755

3856
/**
3957
* No particular reason given. This is currently used for backward compatibility so that
4058
* classes derived from BufferWrite and overriding `getMaxData/0` still work with the
4159
* queries as intended.
4260
*/
43-
class NoSpecifiedEstimateReason extends BufferWriteEstimationReason, TNoSpecifiedEstimateReason {
44-
override string toString() { result = "NoSpecifiedEstimateReason" }
61+
class UnspecifiedEstimateReason extends BufferWriteEstimationReason, TUnspecifiedEstimateReason {
62+
override string toString() { result = "UnspecifiedEstimateReason" }
4563

4664
override string getDescription() { result = "no reason specified" }
47-
48-
override BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other) {
49-
// this reason should not be used in format specifiers, so it should not be combined
50-
// with other reasons
51-
none()
52-
}
5365
}
5466

5567
/**
@@ -60,9 +72,24 @@ class TypeBoundsAnalysis extends BufferWriteEstimationReason, TTypeBoundsAnalysi
6072
override string toString() { result = "TypeBoundsAnalysis" }
6173

6274
override string getDescription() { result = "based on type bounds" }
75+
}
6376

64-
override BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other) {
65-
other != TNoSpecifiedEstimateReason() and result = TTypeBoundsAnalysis()
77+
/**
78+
* The estimation comes from non trivial bounds found via actual flow analysis,
79+
* but a widening aproximation might have been used for variables in loops.
80+
* For example
81+
* ```
82+
* for (int i = 0; i < 10; ++i) {
83+
* int j = i + i;
84+
* //... <- estimation done here based on j
85+
* }
86+
* ```
87+
*/
88+
class WidenedValueFlowAnalysis extends BufferWriteEstimationReason, TWidenedValueFlowAnalysis {
89+
override string toString() { result = "WidenedValueFlowAnalysis" }
90+
91+
override string getDescription() {
92+
result = "based on flow analysis of value bounds with a widening approximation"
6693
}
6794
}
6895

@@ -80,10 +107,6 @@ class ValueFlowAnalysis extends BufferWriteEstimationReason, TValueFlowAnalysis
80107
override string toString() { result = "ValueFlowAnalysis" }
81108

82109
override string getDescription() { result = "based on flow analysis of value bounds" }
83-
84-
override BufferWriteEstimationReason combineWith(BufferWriteEstimationReason other) {
85-
other != TNoSpecifiedEstimateReason() and result = other
86-
}
87110
}
88111

89112
class PrintfFormatAttribute extends FormatAttribute {
@@ -359,6 +382,23 @@ private int lengthInBase10(float f) {
359382
result = f.log10().floor() + 1
360383
}
361384

385+
bindingset[expr]
386+
private BufferWriteEstimationReason getEstimationReasonForIntegralExpression(Expr expr) {
387+
// we consider the range analysis non trivial if it
388+
// * constrained non-trivially both sides of a signed value, or
389+
// * constrained non-trivially the positive side of an unsigned value
390+
// expr should already be given as getFullyConverted
391+
if
392+
upperBound(expr) < exprMaxVal(expr) and
393+
(exprMinVal(expr) >= 0 or lowerBound(expr) > exprMinVal(expr))
394+
then
395+
// next we check whether the estimate may have been widened
396+
if upperBoundMayBeWidened(expr)
397+
then result = TWidenedValueFlowAnalysis()
398+
else result = TValueFlowAnalysis()
399+
else result = TTypeBoundsAnalysis()
400+
}
401+
362402
/**
363403
* A class to represent format strings that occur as arguments to invocations of formatting functions.
364404
*/
@@ -1157,12 +1197,10 @@ class FormatLiteral extends Literal {
11571197
1 + lengthInBase10(2.pow(this.getIntegralDisplayType(n).getSize() * 8 - 1)) and
11581198
// The second case uses range analysis to deduce a length that's shorter than the length
11591199
// of the number -2^31.
1160-
exists(Expr arg, float lower, float upper, float typeLower, float typeUpper |
1200+
exists(Expr arg, float lower, float upper |
11611201
arg = this.getUse().getConversionArgument(n) and
11621202
lower = lowerBound(arg.getFullyConverted()) and
1163-
upper = upperBound(arg.getFullyConverted()) and
1164-
typeLower = exprMinVal(arg.getFullyConverted()) and
1165-
typeUpper = exprMaxVal(arg.getFullyConverted())
1203+
upper = upperBound(arg.getFullyConverted())
11661204
|
11671205
valueBasedBound =
11681206
max(int cand |
@@ -1179,11 +1217,9 @@ class FormatLiteral extends Literal {
11791217
else cand = lengthInBase10(upper)
11801218
)
11811219
) and
1182-
(
1183-
if lower > typeLower or upper < typeUpper
1184-
then reason = TValueFlowAnalysis()
1185-
else reason = TTypeBoundsAnalysis()
1186-
)
1220+
// we don't want to call this on `arg.getFullyConverted()` as we want
1221+
// to detect non-trivial range analysis without taking into account up-casting
1222+
reason = getEstimationReasonForIntegralExpression(arg)
11871223
) and
11881224
len = valueBasedBound.minimum(typeBasedBound)
11891225
)
@@ -1195,12 +1231,10 @@ class FormatLiteral extends Literal {
11951231
typeBasedBound = lengthInBase10(2.pow(this.getIntegralDisplayType(n).getSize() * 8) - 1) and
11961232
// The second case uses range analysis to deduce a length that's shorter than
11971233
// the length of the number 2^31 - 1.
1198-
exists(Expr arg, float lower, float upper, float typeLower, float typeUpper |
1234+
exists(Expr arg, float lower, float upper |
11991235
arg = this.getUse().getConversionArgument(n) and
12001236
lower = lowerBound(arg.getFullyConverted()) and
1201-
upper = upperBound(arg.getFullyConverted()) and
1202-
typeLower = exprMinVal(arg.getFullyConverted()) and
1203-
typeUpper = exprMaxVal(arg.getFullyConverted())
1237+
upper = upperBound(arg.getFullyConverted())
12041238
|
12051239
valueBasedBound =
12061240
lengthInBase10(max(float cand |
@@ -1210,11 +1244,9 @@ class FormatLiteral extends Literal {
12101244
or
12111245
cand = upper
12121246
)) and
1213-
(
1214-
if lower > typeLower or upper < typeUpper
1215-
then reason = TValueFlowAnalysis()
1216-
else reason = TTypeBoundsAnalysis()
1217-
)
1247+
// we don't want to call this on `arg.getFullyConverted()` as we want
1248+
// to detect non-trivial range analysis without taking into account up-casting
1249+
reason = getEstimationReasonForIntegralExpression(arg)
12181250
) and
12191251
len = valueBasedBound.minimum(typeBasedBound)
12201252
)

0 commit comments

Comments
 (0)