1212import ruby
1313import codeql_ruby.DataFlow
1414import DataFlow:: PathGraph
15- private import codeql_ruby.dataflow.SSA
16-
17- // TODO: account for flows through tuple assignments
18- /** An expression referencing the File or FileUtils module */
19- class FileModuleAccess extends Expr {
20- FileModuleAccess ( ) {
21- this .( ConstantAccess ) .getName ( ) = "File"
22- or
23- this .( ConstantAccess ) .getName ( ) = "FileUtils"
24- or
25- exists ( FileModuleAccess fma , Ssa:: WriteDefinition def |
26- def .getARead ( ) = this .getAControlFlowNode ( ) and
27- def .getWriteAccess ( ) .getParent ( ) .( Assignment ) .getRightOperand ( ) = fma
28- )
29- }
30- }
15+ import codeql_ruby.ApiGraphs
3116
3217bindingset [ p]
3318int world_permission ( int p ) { result = p .bitAnd ( 7 ) }
@@ -58,14 +43,13 @@ class PermissivePermissionsExpr extends Expr {
5843}
5944
6045/** A permissions argument of a call to a File/FileUtils method that may modify file permissions */
61- class PermissionArgument extends Expr {
62- private MethodCall call ;
63- private string methodName ;
46+ class PermissionArgument extends DataFlow:: Node {
47+ private DataFlow:: CallNode call ;
6448
6549 PermissionArgument ( ) {
66- call . getReceiver ( ) instanceof FileModuleAccess and
67- call . getMethodName ( ) = methodName and
68- (
50+ exists ( string methodName |
51+ call = API :: moduleImport ( [ "File" , "FileUtils" ] ) . getAMethodCall ( methodName )
52+ |
6953 methodName in [ "chmod" , "chmod_R" , "lchmod" ] and this = call .getArgument ( 0 )
7054 or
7155 methodName = "mkfifo" and this = call .getArgument ( 1 )
@@ -78,7 +62,7 @@ class PermissionArgument extends Expr {
7862 )
7963 }
8064
81- MethodCall getCall ( ) { result = call }
65+ MethodCall getCall ( ) { result = call . asExpr ( ) . getExpr ( ) }
8266}
8367
8468class PermissivePermissionsConfig extends DataFlow:: Configuration {
@@ -88,14 +72,12 @@ class PermissivePermissionsConfig extends DataFlow::Configuration {
8872 exists ( PermissivePermissionsExpr ppe | source .asExpr ( ) .getExpr ( ) = ppe )
8973 }
9074
91- override predicate isSink ( DataFlow:: Node sink ) {
92- exists ( PermissionArgument arg | sink .asExpr ( ) .getExpr ( ) = arg )
93- }
75+ override predicate isSink ( DataFlow:: Node sink ) { sink instanceof PermissionArgument }
9476}
9577
9678from
9779 DataFlow:: PathNode source , DataFlow:: PathNode sink , PermissivePermissionsConfig conf ,
9880 PermissionArgument arg
99- where conf .hasFlowPath ( source , sink ) and arg = sink .getNode ( ) . asExpr ( ) . getExpr ( )
81+ where conf .hasFlowPath ( source , sink ) and arg = sink .getNode ( )
10082select source .getNode ( ) , source , sink , "Overly permissive mask in $@ sets file to $@." ,
10183 arg .getCall ( ) , arg .getCall ( ) .toString ( ) , source .getNode ( ) , source .getNode ( ) .toString ( )
0 commit comments