Repository navigation
Unified: Add command injection query. #22879
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
Draft
geoffw0
wants to merge
7
commits into
github:main
Choose a base branch
from
geoffw0:unifiedcommandinjection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e3adb9
unified: copy command injection query and tests
geoffw0 4a3124e
unified: copy original Swift command injection configuration and models
geoffw0 d9cdc47
unified: port command injection query to a compiling baseline
geoffw0 618b888
unified: model Process.run with its default termination handler
geoffw0 d93f510
unified: refresh command injection guidance and references
geoffw0 21a3e09
unified: Revert most of the changes to the .qhelp (as far as I can te…
geoffw0 2012a65
unified: Delete excessive comment.
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
45 changes: 45 additions & 0 deletions
45
unified/ql/src/queries/security/CWE-078/CommandInjection.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,45 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p> | ||
| Constructing a system command with unsanitized user input is dangerous, | ||
| since a malicious user may be able to craft input that executes arbitrary code. | ||
| </p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| If possible, use hard-coded string literals to specify the command to run. Instead of interpreting | ||
| user input directly as command names, examine the input and then choose among hard-coded string | ||
| literals. | ||
| </p> | ||
| <p> | ||
| If this is not possible, then add sanitization code to verify that the user input is safe before | ||
| using it. | ||
| </p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p> | ||
| The following example executes code from user input without | ||
| sanitizing it first: | ||
| </p> | ||
| <sample src="CommandInjectionBad.swift" /> | ||
| <p> | ||
| If user input is used to construct a command it should be checked | ||
| first. This ensures that the user cannot insert characters that have special | ||
| meanings: | ||
| </p> | ||
| <sample src="CommandInjectionGood.swift" /> | ||
| </example> | ||
|
|
||
| <references> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://community.owasp.org/attacks/Command_Injection">Command Injection</a>. | ||
| </li> | ||
| </references> | ||
| </qhelp> |
43 changes: 43 additions & 0 deletions
43
unified/ql/src/queries/security/CWE-078/CommandInjection.ql
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,43 @@ | ||
| /** | ||
| * @name System command built from user-controlled sources | ||
| * @description Building a system command from user-controlled sources may allow a malicious | ||
| * user to change the meaning of the command. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @security-severity 9.8 | ||
| * @precision high | ||
| * @id unified/swift/command-line-injection | ||
| * @tags security | ||
| * external/cwe/cwe-078 | ||
| * external/cwe/cwe-088 | ||
| */ | ||
|
|
||
| import unified | ||
|
|
||
| /** | ||
| * A taint configuration for tainted data that reaches a command injection sink. | ||
| */ | ||
| module CommandInjectionConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { | ||
| // TODO: Support Process property-write sinks from original Swift's CommandInjectionExtensions.qll. | ||
| Models::isSink(node, "command-injection") | ||
| } | ||
|
|
||
| predicate isBarrier(DataFlow::Node node) { | ||
| // TODO: Port the numeric-type barrier from original Swift's CommandInjectionExtensions.qll. | ||
| none() | ||
| } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module CommandInjectionFlow = TaintTracking::Global<CommandInjectionConfig>; | ||
|
|
||
| import CommandInjectionFlow::PathGraph | ||
|
|
||
| from CommandInjectionFlow::PathNode source, CommandInjectionFlow::PathNode sink | ||
| where CommandInjectionFlow::flowPath(source, sink) | ||
| select sink.getNode(), source, sink, "This command depends on a $@.", source.getNode(), | ||
| "user-provided value" |
5 changes: 5 additions & 0 deletions
5
unified/ql/src/queries/security/CWE-078/CommandInjectionBad.swift
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,5 @@ | ||
| var task = Process() | ||
| task.launchPath = "/bin/bash" | ||
| task.arguments = ["-c", userControlledString] // BAD | ||
|
|
||
| task.launch() |
15 changes: 15 additions & 0 deletions
15
unified/ql/src/queries/security/CWE-078/CommandInjectionGood.swift
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,15 @@ | ||
| func validateCommand(_ command: String) -> String? { | ||
| let allowedCommands = ["ls -l", "pwd", "echo"] | ||
| if allowedCommands.contains(command) { | ||
| return command | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if let validatedString = validateCommand(userControlledString) { | ||
| var task = Process() | ||
| task.launchPath = "/bin/bash" | ||
| task.arguments = ["-c", validatedString] // GOOD | ||
|
|
||
| task.launch() | ||
| } |
29 changes: 29 additions & 0 deletions
29
unified/ql/test/query-tests/security/CWE-078/CommandInjection/CommandInjection.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,29 @@ | ||
| #select | ||
| | CommandInjection.swift:150:42:150:61 | userControlledString | CommandInjection.swift:105:40:105:94 | String(...) | CommandInjection.swift:150:42:150:61 | userControlledString | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | String(...) | user-provided value | | ||
| | CommandInjection.swift:154:23:154:56 | ... ! | CommandInjection.swift:105:40:105:94 | String(...) | CommandInjection.swift:154:23:154:56 | ... ! | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | String(...) | user-provided value | | ||
| | CommandInjection.swift:158:23:158:56 | ... ! | CommandInjection.swift:105:40:105:94 | String(...) | CommandInjection.swift:158:23:158:56 | ... ! | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | String(...) | user-provided value | | ||
| | CommandInjection.swift:163:41:163:74 | ... ! | CommandInjection.swift:105:40:105:94 | String(...) | CommandInjection.swift:163:41:163:74 | ... ! | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | String(...) | user-provided value | | ||
| edges | ||
| | CommandInjection.swift:105:8:105:31 | ... .some(...) [some.0] | CommandInjection.swift:105:12:105:31 | userControlledString | provenance | | | ||
| | CommandInjection.swift:105:12:105:31 | userControlledString | CommandInjection.swift:150:42:150:61 | userControlledString | provenance | | | ||
| | CommandInjection.swift:105:12:105:31 | userControlledString | CommandInjection.swift:154:35:154:54 | userControlledString | provenance | | | ||
| | CommandInjection.swift:105:12:105:31 | userControlledString | CommandInjection.swift:158:35:158:54 | userControlledString | provenance | | | ||
| | CommandInjection.swift:105:12:105:31 | userControlledString | CommandInjection.swift:163:53:163:72 | userControlledString | provenance | | | ||
| | CommandInjection.swift:105:35:105:94 | try? ... [some.0] | CommandInjection.swift:105:8:105:31 | ... .some(...) [some.0] | provenance | | | ||
| | CommandInjection.swift:105:40:105:94 | String(...) | CommandInjection.swift:105:35:105:94 | try? ... [some.0] | provenance | | | ||
| | CommandInjection.swift:154:35:154:54 | userControlledString | CommandInjection.swift:154:23:154:56 | ... ! | provenance | | | ||
| | CommandInjection.swift:158:35:158:54 | userControlledString | CommandInjection.swift:158:23:158:56 | ... ! | provenance | | | ||
| | CommandInjection.swift:163:53:163:72 | userControlledString | CommandInjection.swift:163:41:163:74 | ... ! | provenance | | | ||
| nodes | ||
| | CommandInjection.swift:105:8:105:31 | ... .some(...) [some.0] | semmle.label | ... .some(...) [some.0] | | ||
| | CommandInjection.swift:105:12:105:31 | userControlledString | semmle.label | userControlledString | | ||
| | CommandInjection.swift:105:35:105:94 | try? ... [some.0] | semmle.label | try? ... [some.0] | | ||
| | CommandInjection.swift:105:40:105:94 | String(...) | semmle.label | String(...) | | ||
| | CommandInjection.swift:150:42:150:61 | userControlledString | semmle.label | userControlledString | | ||
| | CommandInjection.swift:154:23:154:56 | ... ! | semmle.label | ... ! | | ||
| | CommandInjection.swift:154:35:154:54 | userControlledString | semmle.label | userControlledString | | ||
| | CommandInjection.swift:158:23:158:56 | ... ! | semmle.label | ... ! | | ||
| | CommandInjection.swift:158:35:158:54 | userControlledString | semmle.label | userControlledString | | ||
| | CommandInjection.swift:163:41:163:74 | ... ! | semmle.label | ... ! | | ||
| | CommandInjection.swift:163:53:163:72 | userControlledString | semmle.label | userControlledString | | ||
| subpaths |
3 changes: 3 additions & 0 deletions
3
unified/ql/test/query-tests/security/CWE-078/CommandInjection/CommandInjection.qlref
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 @@ | ||
| query: queries/security/CWE-078/CommandInjection.ql | ||
| postprocess: | ||
| - utils/test/InlineExpectationsTestQuery.ql |
222 changes: 222 additions & 0 deletions
222
unified/ql/test/query-tests/security/CWE-078/CommandInjection/CommandInjection.swift
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,222 @@ | ||
|
|
||
| // --- stubs --- | ||
|
|
||
| struct URL | ||
| { | ||
| init?(string: String) {} | ||
| init(fileURLWithPath: String) {} | ||
| } | ||
|
|
||
| class NSObject { | ||
| } | ||
|
|
||
| class Process : NSObject { | ||
| var launchPath: String? { get { nil } set {} } | ||
| var arguments: [String]? { get { nil } set {} } | ||
| func launch() {} | ||
|
|
||
| var executableURL: URL? { get { nil } set {} } | ||
| func run() throws {} | ||
|
|
||
| class func launchedProcess(launchPath path: String, arguments: [String]) -> Process { | ||
| return Process() | ||
| } | ||
|
|
||
| class func run(_ url: URL, arguments: [String], terminationHandler: (@Sendable (Process) -> Void)? = nil) throws -> Process { | ||
| return Process() | ||
| } | ||
| } | ||
|
|
||
| extension String { | ||
| init(contentsOf url: URL) throws { | ||
| self.init("") | ||
| } | ||
| } | ||
|
|
||
| class NSUserScriptTask : NSObject { | ||
| typealias CompletionHandler = @Sendable (Error?) -> Void | ||
|
|
||
| init(url: URL) throws {} | ||
| func execute(completionHandler handler: NSUserScriptTask.CompletionHandler? = nil) { } | ||
| } | ||
|
|
||
| class NSUserUnixTask : NSUserScriptTask { | ||
| func execute(withArguments arguments: [String]?, completionHandler handler: NSUserScriptTask.CompletionHandler? = nil) { } | ||
| } | ||
|
|
||
| protocol NSSecureCoding { | ||
| } | ||
|
|
||
| class NSUserAutomatorTask : NSUserScriptTask { | ||
| func execute(withInput input: NSSecureCoding?, completionHandler handler: NSUserScriptTask.CompletionHandler? = nil) { } | ||
|
|
||
| var variables: [String: Any]? { get { return nil } set { } } | ||
| } | ||
|
|
||
| class FileManager : NSObject { | ||
| class var `default`: FileManager { get { return 0 as! FileManager } } | ||
|
|
||
| func contentsOfDirectory(atPath path: String) throws -> [String] { [] } | ||
| } | ||
|
|
||
| // --- tests --- | ||
|
|
||
| func validateCommand(_ command: String) -> String? { | ||
| let allowedCommands = ["ls -l", "pwd", "echo"] | ||
|
|
||
| if allowedCommands.contains(command) { | ||
| return command | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func testCommandInjectionQhelpExamples() { | ||
| guard let userControlledString = try? String(contentsOf: URL(string: "http://example.com/")!) else { // $ MISSING: Source | ||
| return | ||
| } | ||
|
|
||
| let task1 = Process() | ||
| task1.launchPath = "/bin/bash" // GOOD | ||
| task1.arguments = ["-c", userControlledString] // $ MISSING: Alert | ||
| task1.launch() | ||
|
|
||
| if let validatedString = validateCommand(userControlledString) { | ||
| let task2 = Process() | ||
| task2.launchPath = "/bin/bash" // GOOD | ||
| task2.arguments = ["-c", validatedString] // GOOD (original Swift false positive) | ||
| task2.launch() | ||
| } | ||
| } | ||
|
|
||
| func mkProcess() -> Process? { | ||
| return Process() | ||
| } | ||
|
|
||
| class MyProcess : Process { | ||
| var harmlessField: String? | ||
|
|
||
| func setArguments(_ arguments: [String]) { | ||
| self.arguments = arguments // $ MISSING: Alert | ||
| } | ||
| } | ||
|
|
||
| func testCommandInjectionMore(mySafeString: String) { | ||
| guard let userControlledString = try? String(contentsOf: URL(string: "http://example.com/")!) else { // $ Source | ||
| return | ||
| } | ||
|
|
||
| let task1 = Process() | ||
| task1.executableURL = URL(string: mySafeString)! // GOOD | ||
| task1.arguments = ["abc"] // GOOD | ||
| try! task1.run() | ||
|
|
||
| let task2 = Process() | ||
| task2.executableURL = URL(fileURLWithPath: mySafeString) // GOOD | ||
| task2.arguments = ["abc", "def"] // GOOD | ||
| try! task2.run() | ||
|
|
||
| let task3 = Process() | ||
| task3.executableURL = URL(string: userControlledString)! // $ MISSING: Alert | ||
| task3.arguments = ["abc", userControlledString] // $ MISSING: Alert | ||
| try! task3.run() | ||
|
|
||
| let task4 = Process() | ||
| task4.executableURL = URL(fileURLWithPath: userControlledString) // $ MISSING: Alert | ||
| task4.executableURL = URL(string: userControlledString)! // $ MISSING: Alert | ||
| task4.arguments = ["abc", "def" + userControlledString] // $ MISSING: Alert | ||
| try! task4.run() | ||
|
|
||
| let task5 = mkProcess() | ||
| task5?.executableURL = URL(fileURLWithPath: userControlledString) // $ MISSING: Alert | ||
| task5?.arguments = [userControlledString] // $ MISSING: Alert | ||
| try! task5?.run() | ||
|
|
||
| let task6 = MyProcess() | ||
| task6.executableURL = URL(fileURLWithPath: userControlledString) // $ MISSING: Alert | ||
| task6.executableURL = URL(string: userControlledString)! // $ MISSING: Alert | ||
| task6.arguments = [userControlledString] // $ MISSING: Alert | ||
| task6.setArguments([userControlledString]) // BAD (expected inside setArguments; currently missing) | ||
| task6.harmlessField = userControlledString // GOOD | ||
| try! task6.run() | ||
|
|
||
| let task7 = Process() | ||
| task7.executableURL = URL(fileURLWithPath: mySafeString) // GOOD | ||
| task7.arguments = ["abc", "def"] | ||
| task7.arguments?.append(userControlledString) // $ MISSING: Alert // Originally missing | ||
| try! task7.run() | ||
|
|
||
| _ = Process.launchedProcess(launchPath: mySafeString, arguments: ["abc", mySafeString]) // GOOD | ||
| _ = Process.launchedProcess(launchPath: userControlledString, arguments: ["abc", mySafeString]) // $ Alert | ||
| _ = Process.launchedProcess(launchPath: mySafeString, arguments: ["abc", userControlledString]) // $ MISSING: Alert | ||
|
|
||
| _ = try? Process.run(URL(string: mySafeString)!, arguments: ["abc", mySafeString]) // GOOD | ||
| _ = try? Process.run(URL(string: userControlledString)!, arguments: ["abc", mySafeString]) // $ Alert | ||
| _ = try? Process.run(URL(string: mySafeString)!, arguments: ["abc", userControlledString]) // $ MISSING: Alert | ||
|
|
||
| _ = try? Process.run(URL(string: mySafeString)!, arguments: [], terminationHandler: nil) // GOOD | ||
| _ = try? Process.run(URL(string: userControlledString)!, arguments: [], terminationHandler: nil) // $ Alert | ||
|
|
||
| let task8 = try! NSUserScriptTask(url: URL(string: mySafeString)!) // GOOD | ||
| task8.execute() | ||
|
|
||
| let task9 = try! NSUserScriptTask(url: URL(string: userControlledString)!) // $ Alert | ||
| task9.execute() | ||
|
|
||
| let task10 = try! NSUserUnixTask(url: URL(string: userControlledString)!) // $ MISSING: Alert | ||
| task10.execute(withArguments: [userControlledString]) // $ MISSING: Alert | ||
|
|
||
| let task11 = try! NSUserAutomatorTask(url: URL(string: userControlledString)!) // $ MISSING: Alert | ||
| task11.variables = ["abc": userControlledString] // $ MISSING: Alert // Originally missing | ||
| task11.execute(withInput: nil) | ||
|
|
||
| let files = try! FileManager.default.contentsOfDirectory(atPath: "some/directory") | ||
| for file in files { | ||
| let task12 = Process() | ||
| task12.launchPath = "/bin/rm" // GOOD | ||
| task12.arguments = [file] // GOOD (cases like this vary, but our analysis doesn't work well on them) | ||
| task12.launch() | ||
| task12.arguments = files // GOOD (similar to previous) | ||
| task12.launch() | ||
| task12.arguments = [files[0]] // GOOD (similar to previous) | ||
| task12.launch() | ||
| } | ||
|
|
||
| } | ||
|
|
||
| struct MyClass { | ||
| let p1 : Process | ||
| let p2 : Process? | ||
| let p3 : Process! | ||
|
|
||
| var myValue: [String]? { | ||
| get { | ||
| return nil | ||
| } | ||
| set { | ||
| p1.arguments = newValue // $ MISSING: Alert | ||
| p2!.arguments = newValue // $ MISSING: Alert | ||
| p3.arguments = newValue // $ MISSING: Alert | ||
| } | ||
| } | ||
|
|
||
| mutating func myFunc() { | ||
| guard let userControlledString = try? String(contentsOf: URL(string: "http://example.com/")!) else { // $ MISSING: Source | ||
| return | ||
| } | ||
|
|
||
| let tainted1 = [userControlledString] | ||
|
|
||
| p1.arguments = tainted1 // $ MISSING: Alert | ||
| p2!.arguments = tainted1 // $ MISSING: Alert | ||
| p3.arguments = tainted1 // $ MISSING: Alert | ||
|
|
||
| let tainted2 : [String]? = [userControlledString] | ||
|
|
||
| p1.arguments = tainted2 // $ MISSING: Alert | ||
| p2!.arguments = tainted2 // $ MISSING: Alert | ||
| p3.arguments = tainted2 // $ MISSING: Alert | ||
|
|
||
| myValue = tainted2 | ||
| } | ||
| } |
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.
@asgerf Copilot added this near-duplicate of the model below, and it does indeed let us catch an additional test case where the default argument value for
terminationHandleris used. But I suspect we might prefer a general fix for default arguments in the logic that matches models with function calls?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.
See: https://developer.apple.com/documentation/foundation/process/run(_:arguments:terminationhandler:)
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.
Yes, I'm aware of the limitation and expect to make a general fix for this. But it's not a small fix.