Visitar URL original
[CWE-369] Query for divide by zero detection by monkey-junkie · Pull Request #442 · github/codeql-go · GitHub
The Wayback Machine - https://web.archive.org/web/20260507012413/https://github.com/github/codeql-go/pull/442
Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

[CWE-369] Query for divide by zero detection#442

Merged
smowton merged 12 commits intogithub:mainfrom
monkey-junkie:main
Feb 11, 2021
Merged

[CWE-369] Query for divide by zero detection#442
smowton merged 12 commits intogithub:mainfrom
monkey-junkie:main

Conversation

@monkey-junkie
Copy link
Copy Markdown
Contributor

This query adds detection for CWE-369, if integer variable from user input is zero and used as a divisor, it might lead to divide by zero and panic() in golang.
PR contains simple example, documentation and actual query, which works as following:

  • find every results of string-to-int conversions or any other variables with integer type;
  • find if statement, which is used to compare variable with zero;
  • if there are no if's, then fire an alert.

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

This is currently very broad due to considering all integer-typed expressions as sources. Suggest using UntrustedFlowSource as the ultimate source, and using isAdditionalTaintStep to make sure integer-parsing routines are propagating taint in the DivideByZeroCheckConfig. See the implementation of TaintTracking::FunctionModel for an example of how we can get from particular Functions we know should propagate taint to the dataflow nodes for the argument and result.

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
@monkey-junkie
Copy link
Copy Markdown
Contributor Author

monkey-junkie commented Jan 17, 2021 •

Changed to UntrustedFlowSource with isAdditionalTaintStep.
The last case, which came up during debugging - could you please suggest how does it possible to properly detect explicit casts to int via int(variable)?
@smowton could you please take a look?

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Please also add tests exercising the various intended sanitization cases.

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
@smowton
Copy link
Copy Markdown
Contributor

smowton commented Jan 18, 2021

Regarding your question about detecting explicit casts, could you give a brief example program so I'm sure we're talking about the same thing?

Co-authored-by: Chris Smowton <smowton@github.com>
@monkey-junkie
Copy link
Copy Markdown
Contributor Author

monkey-junkie commented Jan 20, 2021 •

Regarding your question about detecting explicit casts, could you give a brief example program so I'm sure we're talking about the same thing?

I would like to detect cases like:
some_const := 0.1
user_input1 := 0.1 // suppose, we can control some value which later will be used in calculations, explicit cast and division
user_input2 := some_const - user_input1
explicit_cast := int(user_input2)
divide_by_zero := (1337 / explicit_cast) // boom

Here is the example of such vulnerability from golang standard library (founded and fixed in 2015).

Please also add tests exercising the various intended sanitization cases.

Could you please specify how such tests should be look like?
Example of golang program with different sanitization cases?
@smowton

@smowton
Copy link
Copy Markdown
Contributor

smowton commented Jan 21, 2021

Understood: you can find the various expression classes here: https://codeql.github.com/codeql-standard-libraries/go/semmle/go/Expr.qll/module.Expr.html

The int(...) expression is: https://codeql.github.com/codeql-standard-libraries/go/semmle/go/Expr.qll/type.Expr$ConversionExpr.html

The dataflow equivalent is TypeCastNode (defined by expr instanceof TypeAssertExpr or expr instanceof ConversionExpr). There is already a default local dataflow rule passing through a conversion which applies to all DataFlow and TaintTracking Configurations.

Thus it looks like the only missing piece is treating ParseFloat as a taint propagator, like ParseInt at the moment. Indeed I was able to get a result with your query this example program:

package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: ./program value\n")
		return
	}
	val1 := 1337
	value, _ := strconv.ParseFloat(os.Args[1], 64)
        ival := int(value)
	out := val1 / ival
	fmt.Println(out)
	return
}

I had to note os.Args as a source though, as by default the Go analysis only considers network-remote sources such as HTTP form fields (you can see this used a lot in our testsuite).

I also had to make some trivial changes to get this to compile -- I'll add them as a review comment in a moment.

The next step should be to add tests -- make a directory under ql/test/experimental with a .qlref file referring to this .ql file, and add Go files that should produce alerts. Then run the tests, which will create a .actual file showing the query results -- once it appears to be right, rename it to .expected. Take a look at some of the other tests for examples to get you going.

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Note BarrierGuard's characteristic predicate (method with Java-constructor-like syntax) should bind this to the whole equality or relational comparison test, then checks should bind e to the expression that is validated when the test passes/fails as indicated by outcome. I also fixed use of hasQualifiedName and removed now-unused exists variables.

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
@monkey-junkie
Copy link
Copy Markdown
Contributor Author

int() conversion detection and test has been added.
@smowton could you please take a look?

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
@smowton
Copy link
Copy Markdown
Contributor

smowton commented Jan 26, 2021

The rule for an int conversion is strange -- as I said, there is already a default local dataflow rule passing through a conversion which applies to all DataFlow and TaintTracking Configurations, so it should not be necessary to make a rule here. The rule also has an odd sink, leading to some child expression of the conversion, which will mean its operand or a subexpression of its operand, whereas I would expect it to propagate to the result.

I suggest trying removing that rule entirely -- does your test fail then? If so, is it possible the array/character accesses are important, not the conversion?

@monkey-junkie
Copy link
Copy Markdown
Contributor Author

monkey-junkie commented Jan 27, 2021 •

I've fixed test (just detection message) and added comments

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Looking good, we're down to style fixes.

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql
@monkey-junkie
Copy link
Copy Markdown
Contributor Author

@smowton

@smowton
Copy link
Copy Markdown
Contributor

smowton commented Jan 29, 2021

Next step is for the security lab to review the quality of the results; you should hear from them soon.

Meanwhile, could you squash all these tiny fix commits together, so our git history is left reasonably tidy?

Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.qhelp Outdated
Comment thread ql/src/experimental/CWE-369/DivideByZero.ql Outdated
Comment thread ql/test/experimental/CWE-369/DivideByZero.go
Your Name and others added 4 commits February 10, 2021 00:26
Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>
Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>
Update ql/src/experimental/CWE-369/DivideByZero.qhelp

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.qhelp

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.qhelp

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>
Your Name and others added 2 commits February 10, 2021 00:26
fix

test fix

Update ql/src/experimental/CWE-369/DivideByZero.ql

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.qhelp

Co-authored-by: Chris Smowton <smowton@github.com>

Update ql/src/experimental/CWE-369/DivideByZero.qhelp

Co-authored-by: Chris Smowton <smowton@github.com>
@monkey-junkie
Copy link
Copy Markdown
Contributor Author

@smowton could you please check?
Thanks!

@smowton smowton merged commit 2d08173 into github:main Feb 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants