Skip to content

Commit 48ee9dd

Browse files
authored
Merge branch 'main' into post-release-prep/codeql-cli-2.23.6
2 parents 76dc4c4 + 958d209 commit 48ee9dd

File tree

12 files changed

+155
-13
lines changed

12 files changed

+155
-13
lines changed

cpp/ql/lib/semmle/code/cpp/Function.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
171171
* Gets the nth parameter of this function. There is no result for the
172172
* implicit `this` parameter, and there is no `...` varargs pseudo-parameter.
173173
*/
174+
pragma[nomagic]
174175
Parameter getParameter(int n) { params(unresolveElement(result), underlyingElement(this), n, _) }
175176

176177
/**
177178
* Gets a parameter of this function. There is no result for the implicit
178179
* `this` parameter, and there is no `...` varargs pseudo-parameter.
179180
*/
181+
pragma[nomagic]
180182
Parameter getAParameter() { params(unresolveElement(result), underlyingElement(this), _, _) }
181183

182184
/**

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Analyser.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,22 @@ private static string Version
360360
return versionString.InformationalVersion;
361361
}
362362
}
363+
364+
private static readonly HashSet<string> errorsToIgnore = new HashSet<string>
365+
{
366+
"CS7027", // Code signing failure
367+
"CS1589", // XML referencing not supported
368+
"CS1569" // Error writing XML documentation
369+
};
370+
371+
/// <summary>
372+
/// Retrieves the diagnostics from the compilation, filtering out those that should be ignored.
373+
/// </summary>
374+
protected List<Diagnostic> GetFilteredDiagnostics() =>
375+
compilation is not null
376+
? compilation.GetDiagnostics()
377+
.Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id))
378+
.ToList()
379+
: [];
363380
}
364381
}

csharp/extractor/Semmle.Extraction.CSharp/Extractor/StandaloneAnalyser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ public StandaloneAnalyser(IProgressMonitor pm, ILogger logger, PathTransformer p
1313
{
1414
}
1515

16+
private void LogDiagnostics()
17+
{
18+
foreach (var error in GetFilteredDiagnostics())
19+
{
20+
Logger.LogDebug($" Compilation error: {error}");
21+
}
22+
}
23+
1624
public void Initialize(string outputPath, IEnumerable<(string, string)> compilationInfos, CSharpCompilation compilationIn, CommonOptions options)
1725
{
1826
compilation = compilationIn;
1927
ExtractionContext = new ExtractionContext(Directory.GetCurrentDirectory(), [], outputPath, compilationInfos, Logger, PathTransformer, ExtractorMode.Standalone, options.QlTest);
2028
this.options = options;
2129
LogExtractorInfo();
2230
SetReferencePaths();
31+
LogDiagnostics();
2332
}
2433
}
2534
}

csharp/extractor/Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,21 @@ internal static string GetOutputName(CSharpCompilation compilation,
136136

137137
private int LogDiagnostics()
138138
{
139-
var filteredDiagnostics = compilation!
140-
.GetDiagnostics()
141-
.Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id))
142-
.ToList();
143-
139+
var filteredDiagnostics = GetFilteredDiagnostics();
144140
foreach (var error in filteredDiagnostics)
145141
{
146142
Logger.LogError($" Compilation error: {error}");
147143
}
148144

149145
if (filteredDiagnostics.Count != 0)
150146
{
151-
foreach (var reference in compilation.References)
147+
foreach (var reference in compilation!.References)
152148
{
153149
Logger.LogInfo($" Resolved reference {reference.Display}");
154150
}
155151
}
156152

157153
return filteredDiagnostics.Count;
158154
}
159-
160-
private static readonly HashSet<string> errorsToIgnore = new HashSet<string>
161-
{
162-
"CS7027", // Code signing failure
163-
"CS1589", // XML referencing not supported
164-
"CS1569" // Error writing XML documentation
165-
};
166155
}
167156
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Compilation errors are now included in the debug log when using build-mode none.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- `CodeQL queries for Rust <https://github.com/github/codeql/tree/main/rust/ql/src>`__
2+
- `Example queries for Rust <https://github.com/github/codeql/tree/main/rust/ql/examples>`__
23
- `CodeQL library reference for Rust <https://codeql.github.com/codeql-standard-libraries/rust/>`__

rust/ql/examples/qlpack.lock.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
dependencies: {}
3+
compiled: false
4+
lockVersion: 1.0.0

rust/ql/examples/qlpack.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: codeql/rust-examples
2+
groups:
3+
- rust
4+
- examples
5+
dependencies:
6+
codeql/rust-all: ${workspace}
7+
warnOnImplicitThis: true
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Empty 'if' expression
3+
* @description Finds 'if' expressions where the "then" branch is empty and no
4+
* "else" branch exists.
5+
* @id rust/examples/empty-if
6+
* @tags example
7+
*/
8+
9+
import rust
10+
11+
// find 'if' expressions...
12+
from IfExpr ifExpr
13+
where
14+
// where the 'then' branch is empty
15+
ifExpr.getThen().getStmtList().getNumberOfStmtOrExpr() = 0 and
16+
// and no 'else' branch exists
17+
not ifExpr.hasElse()
18+
select ifExpr, "This 'if' expression is redundant."
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @name Constant password
3+
* @description Finds places where a string literal is used in a function call
4+
* argument that looks like a password.
5+
* @id rust/examples/simple-constant-password
6+
* @tags example
7+
*/
8+
9+
import rust
10+
import codeql.rust.dataflow.DataFlow
11+
import codeql.rust.dataflow.TaintTracking
12+
13+
/**
14+
* A data flow configuration for tracking flow from a string literal to a function
15+
* call argument that looks like a password. For example:
16+
* ```
17+
* fn set_password(password: &str) { ... }
18+
*
19+
* ...
20+
*
21+
* let pwd = "123456"; // source
22+
* set_password(pwd); // sink (argument 0)
23+
* ```
24+
*/
25+
module ConstantPasswordConfig implements DataFlow::ConfigSig {
26+
predicate isSource(DataFlow::Node node) {
27+
// `node` is a string literal
28+
node.asExpr().getExpr() instanceof StringLiteralExpr
29+
}
30+
31+
predicate isSink(DataFlow::Node node) {
32+
// `node` is an argument whose corresponding parameter name matches the pattern "pass%"
33+
exists(CallExpr call, Function target, int argIndex, Variable v |
34+
call.getStaticTarget() = target and
35+
v.getParameter() = target.getParam(argIndex) and
36+
v.getText().matches("pass%") and
37+
call.getArg(argIndex) = node.asExpr().getExpr()
38+
)
39+
}
40+
}
41+
42+
// instantiate the data flow configuration as a global taint tracking module
43+
module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>;
44+
45+
// report flows from sources to sinks
46+
from DataFlow::Node sourceNode, DataFlow::Node sinkNode
47+
where ConstantPasswordFlow::flow(sourceNode, sinkNode)
48+
select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString()

0 commit comments

Comments
 (0)