Skip to content

C#: New query cs/uncontrolled-format-string - #513

Merged
hvitved merged 5 commits into
github:rc/1.19from
calumgrant:cs/cwe-134
Nov 26, 2018
Merged

C#: New query cs/uncontrolled-format-string#513
hvitved merged 5 commits into
github:rc/1.19from
calumgrant:cs/cwe-134

Conversation

@calumgrant

Copy link
Copy Markdown
Contributor

Should hopefully be straightforward. Note I haven't covered Format-like methods that forward the argument to String.Format -- this would in some cases be handled by normal data-flow anyway.

@calumgrant calumgrant added the C# label Nov 21, 2018
@calumgrant calumgrant added this to the 1.19 milestone Nov 21, 2018
@calumgrant
calumgrant requested review from a team and hvitved November 21, 2018 14:14
@calumgrant
calumgrant requested a review from a team as a code owner November 21, 2018 14:14

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I just have a few suggestions.

import DataFlow::PathGraph

class FormatStringConfiguration extends TaintTracking::Configuration
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move up


override predicate isSink(DataFlow::Node sink) {
exists(MethodCall call | sink.asExpr() = call.getArgumentForName("format") and
call.getTarget() = any(SystemStringClass s).getFormatMethod()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import semmle.code.csharp.frameworks.Format and do

exists(MethodCall call, FormatMethod fm, Parameter p |
  sink.asExpr() = call.getArgumentForParameter(p) |
  call.getTarget() = fm and
  p = fm.getParameter(fm.getFormatArgument())
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really great suggestion. I found FormatCall which made things even simpler.

from FormatStringConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink
where config.hasFlowPath(source, sink)
select sink.getNode(), source, sink,
"$@ flows to here and is used to format 'String.Format'.", source.getNode(), source.getNode().toString()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used to format 'String.Format' -> used as format string ?

@jf205 jf205 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment on the text in the qhelp file - it is a matter of personal preference, so feel free to ignore.
One question about the example you include in the qhelp too - again feel free to ignore.

Otherwise LGTM.

</overview>
<recommendation>

<p>Use a string literal for the format string, to prevent the possibility of data flow from

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete the comma after string.

the application to crash.</p>

<sample src="UncontrolledFormatStringBad.cs" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is very obvious then please ignore this comment, but would it be helpful to include a 'fixed' version of your example here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should normally be the case, but it's quite hard in this case because the fix would require quite a convoluted redesign that I think is beyond the scope of this help. I note that many other security queries don't have the Good version either.

@aibaars
aibaars changed the base branch from master to rc/1.19 November 25, 2018 19:31
@hvitved
hvitved merged commit 7dc0a81 into github:rc/1.19 Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants