|
| 1 | +/** |
| 2 | + * @name NULL application name with an unquoted path in call to CreateProcess |
| 3 | + * @description Calling a function of the CreateProcess* family of functions, where the path contains spaces, introduces a security vulnerability. |
| 4 | + * @id cpp/unsafe-create-process-call |
| 5 | + * @kind problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision medium |
| 8 | + * @msrc.severity important |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-428 |
| 11 | + * external/microsoft/C6277 |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import semmle.code.cpp.dataflow.DataFlow |
| 16 | +import semmle.code.cpp.dataflow.DataFlow2 |
| 17 | + |
| 18 | +predicate isCreateProcessFunction(FunctionCall call, int applicationNameIndex, int commandLineIndex) { |
| 19 | + ( |
| 20 | + call.getTarget().hasGlobalName("CreateProcessA") |
| 21 | + and applicationNameIndex = 0 |
| 22 | + and commandLineIndex = 1 |
| 23 | + ) or ( |
| 24 | + call.getTarget().hasGlobalName("CreateProcessW") |
| 25 | + and applicationNameIndex = 0 |
| 26 | + and commandLineIndex = 1 |
| 27 | + ) or ( |
| 28 | + call.getTarget().hasGlobalName("CreateProcessWithTokenW") |
| 29 | + and applicationNameIndex = 2 |
| 30 | + and commandLineIndex = 3 |
| 31 | + ) or ( |
| 32 | + call.getTarget().hasGlobalName("CreateProcessWithLogonW") |
| 33 | + and applicationNameIndex = 4 |
| 34 | + and commandLineIndex = 5 |
| 35 | + ) or ( |
| 36 | + call.getTarget().hasGlobalName("CreateProcessAsUserA") |
| 37 | + and applicationNameIndex = 1 |
| 38 | + and commandLineIndex = 2 |
| 39 | + ) or ( |
| 40 | + call.getTarget().hasGlobalName("CreateProcessAsUserW") |
| 41 | + and applicationNameIndex = 1 |
| 42 | + and commandLineIndex = 2 |
| 43 | + ) |
| 44 | +} |
| 45 | +/** |
| 46 | + * A function call to CreateProcess (either wide-char or single byte string versions) |
| 47 | + */ |
| 48 | +class CreateProcessFunctionCall extends FunctionCall { |
| 49 | + CreateProcessFunctionCall() { |
| 50 | + isCreateProcessFunction( this, _, _) |
| 51 | + } |
| 52 | + |
| 53 | + int getApplicationNameArgumentId() { |
| 54 | + isCreateProcessFunction( this, result, _) |
| 55 | + } |
| 56 | + |
| 57 | + int getCommandLineArgumentId() { |
| 58 | + isCreateProcessFunction( this, _, result) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Dataflow that detects a call to CreateProcess with a NULL value for lpApplicationName argument |
| 64 | + */ |
| 65 | +class NullAppNameCreateProcessFunctionConfiguration extends DataFlow::Configuration { |
| 66 | + NullAppNameCreateProcessFunctionConfiguration() { |
| 67 | + this = "NullAppNameCreateProcessFunctionConfiguration" |
| 68 | + } |
| 69 | + |
| 70 | + override predicate isSource(DataFlow::Node source) { |
| 71 | + nullValue(source.asExpr()) |
| 72 | + } |
| 73 | + |
| 74 | + override predicate isSink(DataFlow::Node sink) { |
| 75 | + exists( |
| 76 | + CreateProcessFunctionCall call, Expr val | |
| 77 | + val = sink.asExpr() | |
| 78 | + val = call.getArgument(call.getApplicationNameArgumentId()) |
| 79 | + ) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Dataflow that detects a call to CreateProcess with an unquoted commandLine argument |
| 85 | + */ |
| 86 | +class QuotedCommandInCreateProcessFunctionConfiguration extends DataFlow2::Configuration { |
| 87 | + QuotedCommandInCreateProcessFunctionConfiguration() { |
| 88 | + this = "QuotedCommandInCreateProcessFunctionConfiguration" |
| 89 | + } |
| 90 | + |
| 91 | + override predicate isSource(DataFlow2::Node source) { |
| 92 | + exists( string s | |
| 93 | + s = source.asExpr().getValue().toString() |
| 94 | + and |
| 95 | + not isQuotedOrNoSpaceApplicationNameOnCmd(s) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + override predicate isSink(DataFlow2::Node sink) { |
| 100 | + exists( |
| 101 | + CreateProcessFunctionCall call, Expr val | |
| 102 | + val = sink.asExpr() | |
| 103 | + val = call.getArgument(call.getCommandLineArgumentId()) |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +bindingset[s] |
| 109 | +predicate isQuotedOrNoSpaceApplicationNameOnCmd(string s){ |
| 110 | + s.regexpMatch("\"([^\"])*\"(\\s|.)*") // The first element (path) is quoted |
| 111 | + or |
| 112 | + s.regexpMatch("[^\\s]+") // There are no spaces in the string |
| 113 | +} |
| 114 | + |
| 115 | +from CreateProcessFunctionCall call, string msg1, string msg2 |
| 116 | +where |
| 117 | + exists( Expr source, Expr appName, |
| 118 | + NullAppNameCreateProcessFunctionConfiguration nullAppConfig | |
| 119 | + appName = call.getArgument(call.getApplicationNameArgumentId()) |
| 120 | + and nullAppConfig.hasFlow(DataFlow2::exprNode(source), DataFlow2::exprNode(appName)) |
| 121 | + and msg1 = call.toString() + " with lpApplicationName == NULL (" + appName + ")" |
| 122 | + ) |
| 123 | + and |
| 124 | + exists( Expr source, Expr cmd, |
| 125 | + QuotedCommandInCreateProcessFunctionConfiguration quotedConfig | |
| 126 | + cmd = call.getArgument(call.getCommandLineArgumentId()) |
| 127 | + and quotedConfig.hasFlow(DataFlow2::exprNode(source), DataFlow2::exprNode(cmd)) |
| 128 | + and msg2 = " and with an unquoted lpCommandLine (" + cmd + ") introduces a security vulnerability if the path contains spaces." |
| 129 | + ) |
| 130 | +select call, msg1 + " " + msg2 |
0 commit comments