@@ -10,48 +10,89 @@ import semmle.code.java.Collections
1010
1111/**
1212 * Holds if `ex` is used safely as an argument to a command;
13- * i.e. it's not in the first position and it's not a shell command .
13+ * i.e. it's not in a position that selects a command to execute, and the executed command is not a shell.
1414 */
1515predicate isSafeCommandArgument ( Expr ex ) {
1616 exists ( ArrayInit ai , int i |
1717 ex = ai .getInit ( i ) and
1818 i > 0 and
19- not isShell ( ai .getInit ( 0 ) )
19+ not isShell ( ai .getInit ( 0 ) ) and
20+ not exists ( int cmdIndex | cmdIndex = delegatedCommandIndex ( ai .getInit ( 0 ) ) |
21+ i = cmdIndex
22+ or
23+ i > cmdIndex and isShellOrLauncher ( ai .getInit ( cmdIndex ) )
24+ )
2025 )
2126 or
22- exists ( CommandArgumentList cal |
27+ exists ( CommandArgumentList cal , MethodCall add |
2328 not cal .isShell ( ) and
24- ex = cal .getASubsequentAdd ( ) .getArgument ( 0 )
29+ add = cal .getASubsequentAdd ( ) and
30+ ex = add .getArgument ( 0 ) and
31+ not exists ( int cmdIndex | cmdIndex = delegatedCommandIndex ( cal .getAFirstAdd ( ) .getArgument ( 0 ) ) |
32+ add = cal .getAnAddAtIndex ( cmdIndex )
33+ or
34+ not add = cal .getAnAddAtIndex ( [ 1 .. cmdIndex - 1 ] ) and
35+ isShellOrLauncher ( cal .getAnAddAtIndex ( cmdIndex ) .getArgument ( 0 ) )
36+ )
2537 )
2638 or
27- exists ( CommandArgArrayImmutableFirst caa |
39+ exists ( CommandArgArrayImmutableFirst caa , int i |
2840 not caa .isShell ( ) and
29- ex = caa .getAWrite ( any ( int i | i > 0 ) )
41+ i > 0 and
42+ ex = caa .getAWrite ( i ) and
43+ not exists ( int cmdIndex | cmdIndex = delegatedCommandIndex ( caa .getFirstElement ( ) ) |
44+ i = cmdIndex
45+ or
46+ i > cmdIndex and isShellOrLauncher ( caa .getAnElement ( cmdIndex ) )
47+ )
3048 )
3149}
3250
33- /**
34- * Holds if the given expression is the name of a shell command such as bash or python
35- */
36- private predicate isShell ( Expr ex ) {
37- exists ( string cmd | cmd = ex .( StringLiteral ) .getValue ( ) |
38- cmd .regexpMatch ( ".*(sh|javac?|python[23]?|osascript|cmd)(\\.exe)?$" )
39- )
51+ /** Gets a string command name that `ex` may evaluate to. */
52+ private string commandName ( Expr ex ) {
53+ result = ex .( CompileTimeConstantExpr ) .getStringValue ( )
4054 or
4155 exists ( SsaDefinition ssa , SsaExplicitWrite def |
4256 ex = ssa .getARead ( ) and
43- isShell ( def .getDefiningExpr ( ) )
57+ result = commandName ( def .getDefiningExpr ( ) )
4458 |
4559 ssa .getAnUltimateDefinition ( ) = def
4660 or
4761 ssa .( SsaCapturedDefinition ) .getAnUltimateCapturedDefinition ( ) = def
4862 )
4963 or
50- isShell ( ex .( Assignment ) .getRhs ( ) )
64+ result = commandName ( ex .( Assignment ) .getRhs ( ) )
5165 or
52- isShell ( ex .( LocalVariableDeclExpr ) .getInit ( ) )
66+ result = commandName ( ex .( LocalVariableDeclExpr ) .getInit ( ) )
67+ }
68+
69+ /**
70+ * Holds if the given expression is the name of a shell command such as bash or python.
71+ */
72+ private predicate isShell ( Expr ex ) {
73+ commandName ( ex ) .regexpMatch ( ".*(sh|javac?|python[23]?|osascript|cmd)(\\.exe)?$" )
74+ }
75+
76+ /**
77+ * Gets the fixed position of the command executed by a supported delegating launcher.
78+ *
79+ * Only simple invocation forms with a command at the expected position are recognized. Launcher
80+ * options and environment assignments are intentionally not interpreted.
81+ */
82+ private int delegatedCommandIndex ( Expr ex ) {
83+ exists ( string launcher |
84+ launcher = commandName ( ex ) .regexpCapture ( "(.*[/\\\\])?([^/\\\\.]+)(\\.exe)?$" , 2 ) and
85+ (
86+ launcher = [ "env" , "nice" , "nohup" , "setsid" , "sudo" , "xargs" ] and result = 1
87+ or
88+ launcher = [ "chroot" , "stdbuf" , "timeout" ] and result = 2
89+ )
90+ )
5391}
5492
93+ /** Holds if `ex` may evaluate to a shell or a supported delegating launcher. */
94+ private predicate isShellOrLauncher ( Expr ex ) { isShell ( ex ) or exists ( delegatedCommandIndex ( ex ) ) }
95+
5596/**
5697 * A type that could be a list of strings. Includes raw `List` types.
5798 */
@@ -107,6 +148,37 @@ private class CommandArgumentList extends SsaExplicitWrite {
107148 not result = this .getAFirstAdd ( )
108149 }
109150
151+ /**
152+ * Gets a use of the variable at which the list could have `index` elements,
153+ * for `index` in `0 .. 2`.
154+ */
155+ private VarRead getAUseAtIndex ( int index ) {
156+ index = 0 and result = this .getAUseBeforeFirstAdd ( )
157+ or
158+ index in [ 1 .. 2 ] and
159+ exists ( VarRead mid |
160+ adjacentUseUse ( mid , result ) and
161+ (
162+ mid = this .getAUseAtIndex ( index - 1 ) and
163+ exists ( MethodCall ma |
164+ mid = ma .getQualifier ( ) and
165+ ma .getMethod ( ) .hasName ( "add" ) and
166+ ma .getNumArgument ( ) = 1
167+ )
168+ or
169+ mid = this .getAUseAtIndex ( index ) and
170+ not mid = this .getAnAdd ( ) .getQualifier ( )
171+ )
172+ )
173+ }
174+
175+ /** Gets an addition to this list that could start at `index`, for `index` in `1 .. 2`. */
176+ MethodCall getAnAddAtIndex ( int index ) {
177+ index in [ 1 .. 2 ] and
178+ result = this .getAnAdd ( ) and
179+ result .getQualifier ( ) = this .getAUseAtIndex ( index )
180+ }
181+
110182 /** Holds if the first element of this list is a shell command. */
111183 predicate isShell ( ) {
112184 exists ( MethodCall ma | ma = this .getAFirstAdd ( ) and isShell ( ma .getArgument ( 0 ) ) )
@@ -117,7 +189,10 @@ private class CommandArgumentList extends SsaExplicitWrite {
117189 * The type `String[]`.
118190 */
119191private class ArrayOfStringType extends Array {
120- ArrayOfStringType ( ) { this .getElementType ( ) instanceof TypeString }
192+ ArrayOfStringType ( ) {
193+ this .getDimension ( ) = 1 and
194+ this .getElementType ( ) instanceof TypeString
195+ }
121196}
122197
123198private predicate arrayVarWrite ( ArrayAccess acc ) { exists ( Assignment a | a .getDest ( ) = acc ) }
@@ -149,54 +224,59 @@ private class CommandArgumentArray extends SsaExplicitWrite {
149224}
150225
151226/**
152- * A `CommandArgArray ` whose element at index 0 is never written to, except possibly once to initialise it.
227+ * A `CommandArgumentArray ` whose element at index 0 is never written to, except possibly once to initialise it.
153228 */
154229private class CommandArgArrayImmutableFirst extends CommandArgumentArray {
155230 CommandArgArrayImmutableFirst ( ) {
156- ( exists ( this .getAWrite ( 0 ) ) or exists ( firstElementOf ( this .getDefiningExpr ( ) ) ) ) and
231+ ( exists ( this .getAWrite ( 0 ) ) or exists ( arrayElementOf ( this .getDefiningExpr ( ) , 0 ) ) ) and
157232 forall ( VarRead use | exists ( this .getAWrite ( 0 , use ) ) | use = ssaGetAFirstUse ( this ) )
158233 }
159234
160- /** Gets the first element of this array. */
161- Expr getFirstElement ( ) {
162- result = this .getAWrite ( 0 )
163- or
164- not exists ( this .getAWrite ( 0 ) ) and
165- result = firstElementOf ( this .getDefiningExpr ( ) )
235+ /** Gets an expression that may be stored at `index`, for `index` in `0 .. 2`. */
236+ Expr getAnElement ( int index ) {
237+ index in [ 0 .. 2 ] and
238+ (
239+ result = this .getAWrite ( index )
240+ or
241+ ( index > 0 or not exists ( this .getAWrite ( 0 ) ) ) and
242+ result = arrayElementOf ( this .getDefiningExpr ( ) , index )
243+ )
166244 }
167245
246+ /** Gets the first element of this array. */
247+ Expr getFirstElement ( ) { result = this .getAnElement ( 0 ) }
248+
168249 /** Holds if the first element of this array is a shell command. */
169250 predicate isShell ( ) { isShell ( this .getFirstElement ( ) ) }
170251}
171252
172- /** Gets the first element of an imutable array of strings */
173- private Expr firstElementOf ( Expr arr ) {
253+ /** Gets an expression for the element at `index` of an array of strings `arr`, for `index` in `0 .. 2`. */
254+ private Expr arrayElementOf ( Expr arr , int index ) {
255+ index in [ 0 .. 2 ] and
174256 arr .getType ( ) instanceof ArrayOfStringType and
175257 (
176- result = firstElementOf ( arr .( Assignment ) .getRhs ( ) )
258+ result = arrayElementOf ( arr .( Assignment ) .getRhs ( ) , index )
177259 or
178- result = firstElementOf ( arr .( LocalVariableDeclExpr ) .getInit ( ) )
260+ result = arrayElementOf ( arr .( LocalVariableDeclExpr ) .getInit ( ) , index )
179261 or
180262 exists ( CommandArgArrayImmutableFirst caa | arr = caa .getARead ( ) |
181- result = caa .getFirstElement ( )
263+ result = caa .getAnElement ( index )
182264 )
183265 or
184- exists ( MethodCall ma , Method m |
266+ exists ( MethodCall ma |
185267 arr = ma and
186- ma .getMethod ( ) = m and
187- m .getDeclaringType ( ) .hasQualifiedName ( "java.util" , "Arrays" ) and
188- m .hasName ( "copyOf" ) and
189- result = firstElementOf ( ma .getArgument ( 0 ) )
268+ ma .getMethod ( ) .hasQualifiedName ( "java.util" , "Arrays" , "copyOf" ) and
269+ result = arrayElementOf ( ma .getArgument ( 0 ) , index )
190270 )
191271 or
192272 exists ( Field f |
193273 f .isStatic ( ) and
194274 arr .( FieldRead ) .getField ( ) = f and
195- result = firstElementOf ( f .getInitializer ( ) )
275+ result = arrayElementOf ( f .getInitializer ( ) , index )
196276 )
197277 or
198- result = arr .( ArrayInit ) .getInit ( 0 )
278+ result = arr .( ArrayInit ) .getInit ( index )
199279 or
200- result = arr .( ArrayCreationExpr ) .getInit ( ) .getInit ( 0 )
280+ result = arr .( ArrayCreationExpr ) .getInit ( ) .getInit ( index )
201281 )
202282}
0 commit comments