Skip to content

The self-implemented taintloop rule has an empty detection result. Is there a good debugging method? #19163

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

Closed
ysuLihua opened this issue Mar 31, 2025 · 5 comments
Labels
question Further information is requested

Comments

@ysuLihua
Copy link

test code:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

void bad1(){
    int factor = atoi(getenv("BRANCHING_FACTOR"));
    int i;
    for(i = 0; i<factor; i++){
        printf("sfasdfad");
    }
}


void bad2(){
    int factor = atoi(getenv("BRANCHING_FACTOR"));
    int i = 0;
    while (i < factor)
    {
        printf("sfasdfad");
        i++;
    }
}

int main(){

}

TaintedLoop.ql

/**
 * @name Untrusted input for a condition
 * @description Using untrusted inputs in a statement that makes a
 *              security decision makes code vulnerable to
 *              attack.
 * @kind path-problem
 * @problem.severity warning
 * @security-severity 7.5
 * @precision medium
 * @id cpp/tainted-loop-check
 * @tags security
 *       external/cwe/cwe-606
 */

import cpp
import semmle.code.cpp.security.Security
import semmle.code.cpp.security.FlowSources
import semmle.code.cpp.ir.dataflow.TaintTracking
import semmle.code.cpp.ir.IR
import Flow::PathGraph

predicate sensitiveCondition(Expr condition) {
  exists(ForStmt forstmt |
    forstmt.getCondition() = condition
  )
}


predicate isSource(FlowSource source, string sourceType) { sourceType = source.getSourceType() }

module Config implements DataFlow::ConfigSig {
  predicate isSource(DataFlow::Node node) { isSource(node, _) }

  predicate isSink(DataFlow::Node node) {
    sensitiveCondition(node.asExpr())
  }

}

module Flow = TaintTracking::Global<Config>;


from
  string sourceType, DataFlow::Node source, DataFlow::Node sink,
  Flow::PathNode sourceNode, Flow::PathNode sinkNode
where
  source = sourceNode.getNode() and
  sink = sinkNode.getNode() and
  isSource(source, sourceType) and
  sensitiveCondition(sink.asExpr()) and
  Flow::flowPath(sourceNode, sinkNode)
select sink, sourceNode, sinkNode, "Taint data to loop condition"


But the SARIF results is None. How can I debug and resolve this problem?

@ysuLihua ysuLihua added the question Further information is requested label Mar 31, 2025
@jketema
Copy link
Contributor

jketema commented Mar 31, 2025

Hi @ysuLihua

This:

 predicate sensitiveCondition(Expr condition) {
   exists(ForStmt forstmt |
     forstmt.getCondition() = condition
   )
 }

looks incorrect, as factor is not the condition itself, but a child of the condition. You probably want to write something like:

 predicate sensitiveCondition(Expr condition) {
   exists(ForStmt forstmt |
     forstmt.getCondition().getAChild*() = condition
   )
 }

@ysuLihua
Copy link
Author

ysuLihua commented Apr 1, 2025

@jketema Brother
After modification, the result is still empty.How can i printout the sinknode expr.

@jketema
Copy link
Contributor

jketema commented Apr 1, 2025

Hi @ysuLihua

I'm not your "Brother", please keep the conversation respectful.

With the above change to sensitiveCondition I get precisely one result. Are you sure you correctly modified your query?
Image

@ysuLihua
Copy link
Author

ysuLihua commented Apr 1, 2025

Hi @jketema ,Thank you very much!
The problem is solved.

@jketema
Copy link
Contributor

jketema commented Apr 1, 2025

Great to hear. Closing this issue as completed.

@jketema jketema closed this as completed Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants