-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Implement a new query for Log Injection #20221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d954b50
Initial plan
Copilot 39ea507
Implement Rust log injection query and test infrastructure
Copilot d72efc5
Final validation and cleanup of Rust log injection query
Copilot 2a19a17
Rust: Run test, accept .expected and Cargo.lock.
geoffw0 49265b6
Rust: Update inline test annotations accordingly.
geoffw0 7b1aa23
Address PR feedback: trim examples, remove duplicate CWE ref, autoformat
Copilot 9836592
Rust: Fix compilation errors in example code.
geoffw0 4328ed8
Rust: Update suite lists.
geoffw0 9e4f59c
Rust: Accept consistency check failures.
geoffw0 bc0d327
Rust: Add log injection sinks to stats.
geoffw0 f05d815
Rust: Update the security-severity tag.
geoffw0 265c2e3
Rust: Change note.
geoffw0 e84135a
Update rust/ql/src/queries/security/CWE-117/LogInjection.qhelp
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
rust/ql/lib/codeql/rust/security/LogInjectionExtensions.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Provides classes and predicates for reasoning about log injection | ||
* vulnerabilities. | ||
*/ | ||
|
||
import rust | ||
private import codeql.rust.dataflow.DataFlow | ||
private import codeql.rust.dataflow.FlowSink | ||
private import codeql.rust.Concepts | ||
private import codeql.util.Unit | ||
|
||
/** | ||
* Provides default sources, sinks and barriers for detecting log injection | ||
* vulnerabilities, as well as extension points for adding your own. | ||
*/ | ||
module LogInjection { | ||
/** | ||
* A data flow source for log injection vulnerabilities. | ||
*/ | ||
abstract class Source extends DataFlow::Node { } | ||
|
||
/** | ||
* A data flow sink for log injection vulnerabilities. | ||
*/ | ||
abstract class Sink extends QuerySink::Range { | ||
override string getSinkType() { result = "LogInjection" } | ||
} | ||
|
||
/** | ||
* A barrier for log injection vulnerabilities. | ||
*/ | ||
abstract class Barrier extends DataFlow::Node { } | ||
|
||
/** | ||
* An active threat-model source, considered as a flow source. | ||
*/ | ||
private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { } | ||
|
||
/** | ||
* A sink for log-injection from model data. | ||
*/ | ||
private class ModelsAsDataSink extends Sink { | ||
ModelsAsDataSink() { sinkNode(this, "log-injection") } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: newQuery | ||
--- | ||
* Added a new query, `rust/log-injection`, for detecting cases where log entries could be forged by a malicious user. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
|
||
<p>If unsanitized user input is written to a log entry, a malicious user may be able to forge new log entries.</p> | ||
|
||
<p>Forgery can occur if a user provides some input with characters that are interpreted | ||
when the log output is displayed. If the log is displayed as a plain text file, then new | ||
line characters can be used by a malicious user. If the log is displayed as HTML, then | ||
arbitrary HTML may be included to spoof log entries.</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p> | ||
User input should be suitably sanitized before it is logged. | ||
</p> | ||
<p> | ||
If the log entries are in plain text, then line breaks should be removed from user input using | ||
<code>String::replace</code> or similar. Care should also be taken that user input is clearly marked | ||
in log entries. | ||
</p> | ||
<p> | ||
For log entries that will be displayed in HTML, user input should be HTML-encoded before being logged, to prevent forgery and | ||
other forms of HTML injection. | ||
</p> | ||
|
||
</recommendation> | ||
|
||
<example> | ||
<p>In the first example, a username, provided by the user via command line arguments, is logged using the <code>log</code> crate. | ||
If a malicious user provides <code>Guest\n[INFO] User: Admin\n</code> as a username parameter, | ||
the log entry will be split into multiple lines, where the second line will appear as <code>[INFO] User: Admin</code>, | ||
potentially forging a legitimate admin login entry. | ||
</p> | ||
<sample src="LogInjectionBad.rs" /> | ||
|
||
<p>In the second example, <code>String::replace</code> is used to ensure no line endings are present in the user input before logging.</p> | ||
<sample src="LogInjectionGood.rs" /> | ||
</example> | ||
|
||
<references> | ||
<li>OWASP: <a href="https://owasp.org/www-community/attacks/Log_Injection">Log Injection</a>.</li> | ||
</references> | ||
</qhelp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @name Log injection | ||
* @description Building log entries from user-controlled sources is vulnerable to | ||
* insertion of forged log entries by a malicious user. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @security-severity 2.6 | ||
* @precision medium | ||
* @id rust/log-injection | ||
* @tags security | ||
* external/cwe/cwe-117 | ||
*/ | ||
|
||
import rust | ||
import codeql.rust.dataflow.DataFlow | ||
import codeql.rust.dataflow.TaintTracking | ||
import codeql.rust.security.LogInjectionExtensions | ||
|
||
/** | ||
* A taint configuration for tainted data that reaches a log injection sink. | ||
*/ | ||
module LogInjectionConfig implements DataFlow::ConfigSig { | ||
import LogInjection | ||
|
||
predicate isSource(DataFlow::Node node) { node instanceof Source } | ||
|
||
predicate isSink(DataFlow::Node node) { node instanceof Sink } | ||
|
||
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof Barrier } | ||
|
||
predicate observeDiffInformedIncrementalMode() { any() } | ||
} | ||
|
||
module LogInjectionFlow = TaintTracking::Global<LogInjectionConfig>; | ||
|
||
import LogInjectionFlow::PathGraph | ||
|
||
from LogInjectionFlow::PathNode sourceNode, LogInjectionFlow::PathNode sinkNode | ||
where LogInjectionFlow::flowPath(sourceNode, sinkNode) | ||
select sinkNode.getNode(), sourceNode, sinkNode, "Log entry depends on a $@.", sourceNode.getNode(), | ||
"user-provided value" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::env; | ||
use log::info; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
|
||
// Get username from command line arguments | ||
let args: Vec<String> = env::args().collect(); | ||
let username = args.get(1).unwrap_or(&String::from("Guest")).clone(); | ||
|
||
// BAD: log message constructed with unsanitized user input | ||
info!("User login attempt: {}", username); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::env; | ||
use log::info; | ||
|
||
fn sanitize_for_logging(input: &str) -> String { | ||
// Remove newlines and carriage returns to prevent log injection | ||
input.replace('\n', "").replace('\r', "") | ||
} | ||
|
||
fn main() { | ||
env_logger::init(); | ||
|
||
// Get username from command line arguments | ||
let args: Vec<String> = env::args().collect(); | ||
let username = args.get(1).unwrap_or(&String::from("Guest")).clone(); | ||
|
||
// GOOD: log message constructed with sanitized user input | ||
let sanitized_username = sanitize_for_logging(username.as_str()); | ||
info!("User login attempt: {}", sanitized_username); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
rust/ql/test/query-tests/security/CWE-117/CONSISTENCY/PathResolutionConsistency.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
multipleCallTargets | ||
| main.rs:9:43:9:63 | ...::from(...) | | ||
| main.rs:44:19:44:32 | username.len() | |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The security severity should be 7.8 according to the PR description, not 2.6. This inconsistency could lead to incorrect risk assessment.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generated it with the
cwe-scores
tool. I appreciate that the number differs from other versions of the query, I haven't looked into why but I'm guessing its because something the tool bases its score on has changed since they were created.