Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative cli tool to make use of the query capabilities #1626

Draft
wants to merge 44 commits into
base: main
Choose a base branch
from

Conversation

MariusAlbrecht
Copy link
Collaborator

@MariusAlbrecht MariusAlbrecht commented Jul 22, 2024

Declarative CLI Tool to Make Use of the Query Capabilities

This pull request introduces a CLI to use Queries outside of the interactive console in a declarative way.
Should work similiar to regular security checkers, insofar it should be able to just be run on a codebase and report something. To do this predefined Rules are neccesary.
Should also be extensible / customizable. To do this the Rule and Reporter interface can be manually implemented to add new rules and output formats.

Usage

Right now the code lives in cpg-analysis (which I acknowledge should probably change).
To actually use

  1. add a Rule to the RuleRunner class (see below for an example)
  2. do :installDist and run the binary which is then located at cpg/cpg-analysis/build/install/cpg-analysis/bin/cpg-analysis

Notes

This is still absolutely WIP, just wanted to get this mostly working thing in a PR before being away for holiday. There's a number of actual "TODO" comments in the code, additionally the following should probably be addressed as well

  • logging
  • tests
  • actual Rules
  • in sarif reports the "main" location of the finding isn't properly handled
  • at least in sarif, dynamic messages (e.g. adding variable names into a predefined message) are possible. The Rule interface supports this, it's up to the Rule to supply the arguments. This could be explored to improve ux

I plan to adddress all of this once I'm back in 3 weeks.


Rule exmaple:

/*
   <copyright> 
   package
*/

import de.fraunhofer.aisec.cpg.TranslationResult
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.query.*

class BufferOverreadMemcpy : Rule {
    override var queryResult: QueryTree<*>? = null // set by run() method of Rule interface
    override val id = "cpg-0000"
    override val name = "memcpy src smaller than size"
    override val shortDescription =
        "This rule detects memcpy calls where the size of the source is smaller than the size argument, which can " +
            "overread the src buffer"
    override val cweId: String = "787"
    override val level = Rule.Level.Error
    override val message = "memcpy call with source size smaller than size argument detected"

    override fun run(result: TranslationResult) {
        queryResult =
            result.allExtended<CallExpression>(
                { it.name.localName == "memcpy" },
                //          src                     n
                { sizeof(it.arguments[1]) ge min(it.arguments[2]) }
            )
    }
}

note that this only gives pretty output and evaluation steps when the used sizeof and min functions are adjusted to include nodes in their children. Just do mutableListOf(QueryTree(n)) instead of mutableListOf() in these 2 locations:

return QueryTree(eval.evaluate(n) as? Int ?: -1, mutableListOf(), "sizeof($n)")

return QueryTree((evalRes as? NumberSet)?.min() ?: -1, mutableListOf(), "min($n)")

utility to easily run rules. For now only for testing, maybe usable as something else later on
(cherry picked from commit 6d197b8)
(cherry picked from commit a559a3d)
utility to easily run rules. For now only for testing, maybe usable as something else later on

(cherry picked from commit f3c668c)
(cherry picked from commit d1f9e99)
(cherry picked from commit 1fdcaca)
(cherry picked from commit 59eae97)
(cherry picked from commit d9ec7ee)
(cherry picked from commit 6fd818f)
@KuechA
Copy link
Contributor

KuechA commented Jul 25, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants