Skip to content

python taint tracking doesn't work with namespace packages properly  #11780

Open
@am0o0

Description

@am0o0

add these files under same main directory
a.py

import cgi
import b
req = cgi.FieldStorage()
ssh = b.cmd_exec(req.getvalue('cmd'))

b.py

def cmd_exec(cmd):
	from mymodule import exec_mymodule
	cmd_output = exec_mymodule("bach"," -c",cmd)
	return cmd_output

Run the following simple taint tracking path query

/**
 * @name myTaint
 * @description myTaint
 * @kind path-problem
 * @problem.severity error
 * @security-severity 9.3
 * @sub-severity high
 * @precision high
 * @id py/myTaint
 * @tags security
 *       external/cwe/cwe-094
 *       external/cwe/cwe-095
 *       external/cwe/cwe-116
 */
import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.TaintTracking
import semmle.python.dataflow.new.RemoteFlowSources
import DataFlow::PathGraph
import semmle.python.ApiGraphs
class Configuration extends TaintTracking::Configuration {
  Configuration() { this = "Configuration"}

  override predicate isSource(DataFlow::Node source) {
    source = API::moduleImport("cgi").getMember("FieldStorage").getReturn().asSource()
  }

  override predicate isSink(DataFlow::Node sink) {
      any()
  }
  }

  from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
  where config.hasFlowPath(source, sink)
  select sink.getNode(), source, sink, "This code execution depends on a $@.", source.getNode(),
    "user-provided value"

You can see that flow can't go into b.cmd_exec from a.py
@jketema on the GHSL slack said that if I change the directory structure as follows:

code
 |- a.py
 |- pack
     |- __init__.py
     |- b.py 

It works, and I tested it, and it has worked for me, and I could track cmd to exec_mymodule as the final Sink.

Metadata

Metadata

Assignees

No one assigned

    Labels

    PythonbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions