|
| 1 | +/** |
| 2 | + * @name Overly permissive file permissions |
| 3 | + * @description Allowing files to be readable or writable by users other than the owner may allow sensitive information to be accessed. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id rb/overly-permissive-file |
| 7 | + * @tags external/cwe/cwe-732 |
| 8 | + * security |
| 9 | + * @precision low |
| 10 | + */ |
| 11 | + |
| 12 | +import ruby |
| 13 | +import codeql_ruby.DataFlow |
| 14 | +import 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 | +} |
| 31 | + |
| 32 | +bindingset[p] |
| 33 | +int world_permission(int p) { result = p.bitAnd(7) } |
| 34 | + |
| 35 | +// 70 oct = 56 dec |
| 36 | +bindingset[p] |
| 37 | +int group_permission(int p) { result = p.bitAnd(56) } |
| 38 | + |
| 39 | +bindingset[p] |
| 40 | +string access(int p) { |
| 41 | + p.bitAnd(2) != 0 and result = "writable" |
| 42 | + or |
| 43 | + p.bitAnd(4) != 0 and result = "readable" |
| 44 | +} |
| 45 | + |
| 46 | +/** An expression specifing a file permission that allows group/others read or write access */ |
| 47 | +class PermissivePermissionsExpr extends Expr { |
| 48 | + // TODO: non-literal expressions? |
| 49 | + PermissivePermissionsExpr() { |
| 50 | + exists(int perm, string acc | |
| 51 | + perm = this.(IntegerLiteral).getValue() and |
| 52 | + (acc = access(world_permission(perm)) or acc = access(group_permission(perm))) |
| 53 | + ) |
| 54 | + or |
| 55 | + // adding/setting read or write permissions for all/group/other |
| 56 | + this.(StringLiteral).getValueText().regexpMatch(".*[ago][^-=+]*[+=][xXst]*[rw].*") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** A call to a method of File or FileUtils that may modify file permissions */ |
| 61 | +class PermissionSettingMethodCall extends MethodCall { |
| 62 | + private string methodName; |
| 63 | + private Expr permArg; |
| 64 | + |
| 65 | + PermissionSettingMethodCall() { |
| 66 | + this.getReceiver() instanceof FileModuleAccess and |
| 67 | + this.getMethodName() = methodName and |
| 68 | + ( |
| 69 | + methodName in ["chmod", "chmod_R", "lchmod"] and permArg = this.getArgument(0) |
| 70 | + or |
| 71 | + methodName = "mkfifo" and permArg = this.getArgument(1) |
| 72 | + or |
| 73 | + methodName in ["new", "open"] and permArg = this.getArgument(2) |
| 74 | + or |
| 75 | + methodName in ["install", "makedirs", "mkdir", "mkdir_p", "mkpath"] and |
| 76 | + permArg = this.getKeywordArgument("mode") |
| 77 | + // TODO: defaults for optional args? This may depend on the umask |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + Expr getPermissionArgument() { result = permArg } |
| 82 | +} |
| 83 | + |
| 84 | +class PermissivePermissionsConfig extends DataFlow::Configuration { |
| 85 | + PermissivePermissionsConfig() { this = "PermissivePermissionsConfig" } |
| 86 | + |
| 87 | + override predicate isSource(DataFlow::Node source) { |
| 88 | + exists(PermissivePermissionsExpr ppe | source.asExpr().getExpr() = ppe) |
| 89 | + } |
| 90 | + |
| 91 | + override predicate isSink(DataFlow::Node sink) { |
| 92 | + exists(PermissionSettingMethodCall c | sink.asExpr().getExpr() = c.getPermissionArgument()) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +from DataFlow::PathNode source, DataFlow::PathNode sink, PermissivePermissionsConfig conf |
| 97 | +where conf.hasFlowPath(source, sink) |
| 98 | +select sink.getNode(), source, sink, "Overly permissive mask sets file to $@.", source.getNode(), |
| 99 | + source.getNode().toString() |
0 commit comments