Skip to content

Commit 4a0d7c5

Browse files
committed
Add top-level CLI injection query and tests
1 parent 8440fe2 commit 4a0d7c5

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

ql/lib/codeql/ruby/security/CommandInjectionCustomizations.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ module CommandInjection {
3939
* A command argument to a function that initiates an operating system command.
4040
*/
4141
class SystemCommandExecutionSink extends Sink, DataFlow::Node {
42-
SystemCommandExecutionSink() { this instanceof SystemCommandExecution }
42+
SystemCommandExecutionSink() { this = any(SystemCommandExecution c).getAnArgument() }
4343
}
4444
}

ql/lib/codeql/ruby/security/CommandInjectionQuery.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import ruby
11-
// import IndirectCommandArgument
1211
import codeql.ruby.TaintTracking
1312
import CommandInjectionCustomizations::CommandInjection
1413
import codeql.ruby.DataFlow
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @name Uncontrolled command line
3+
* @description Using externally controlled strings in a command line may allow a malicious
4+
* user to change the meaning of the command.
5+
* @kind path-problem
6+
* @problem.severity error
7+
* @security-severity 9.8
8+
* @precision high
9+
* @id rb/command-line-injection
10+
* @tags correctness
11+
* security
12+
* external/cwe/cwe-078
13+
* external/cwe/cwe-088
14+
*/
15+
16+
import ruby
17+
import codeql.ruby.security.CommandInjectionQuery
18+
import DataFlow::PathGraph
19+
20+
from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink, Source sourceNode
21+
where
22+
config.hasFlowPath(source, sink) and
23+
sourceNode = source.getNode()
24+
select sink.getNode(), source, sink, "This command depends on $@.", sourceNode,
25+
sourceNode.getSourceType()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
edges
2+
| CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:4:10:4:15 | #{...} |
3+
| CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:5:16:5:18 | cmd |
4+
| CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:6:14:6:16 | cmd |
5+
| CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:7:12:7:17 | #{...} |
6+
nodes
7+
| CommandInjection.rb:3:15:3:20 | call to params : | semmle.label | call to params : |
8+
| CommandInjection.rb:4:10:4:15 | #{...} | semmle.label | #{...} |
9+
| CommandInjection.rb:5:16:5:18 | cmd | semmle.label | cmd |
10+
| CommandInjection.rb:6:14:6:16 | cmd | semmle.label | cmd |
11+
| CommandInjection.rb:7:12:7:17 | #{...} | semmle.label | #{...} |
12+
#select
13+
| CommandInjection.rb:4:10:4:15 | #{...} | CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:4:10:4:15 | #{...} | This command depends on $@. | CommandInjection.rb:3:15:3:20 | call to params | a user-provided value |
14+
| CommandInjection.rb:5:16:5:18 | cmd | CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:5:16:5:18 | cmd | This command depends on $@. | CommandInjection.rb:3:15:3:20 | call to params | a user-provided value |
15+
| CommandInjection.rb:6:14:6:16 | cmd | CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:6:14:6:16 | cmd | This command depends on $@. | CommandInjection.rb:3:15:3:20 | call to params | a user-provided value |
16+
| CommandInjection.rb:7:12:7:17 | #{...} | CommandInjection.rb:3:15:3:20 | call to params : | CommandInjection.rb:7:12:7:17 | #{...} | This command depends on $@. | CommandInjection.rb:3:15:3:20 | call to params | a user-provided value |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-078/CommandInjection.ql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class UsersController < ActionController::Base
2+
def create
3+
cmd = params[:cmd]
4+
`#{cmd}`
5+
system(cmd)
6+
exec(cmd)
7+
%x(#{cmd})
8+
end
9+
10+
def show
11+
`ls`
12+
system("ls")
13+
exec("ls")
14+
%x(ls)
15+
end
16+
end

0 commit comments

Comments
 (0)