pyre-check icon indicating copy to clipboard operation
pyre-check copied to clipboard

Tainted lists

Open 1one-w01f opened this issue 7 years ago • 10 comments

Looks like if one construct a new list with tainted objects, the new list won't be considered tainted?

Consider the following simple example: mymod.py

def foo():
    return [{'type': 'disconnect', 'text':'bye cruel world'}]
    # this is to mimic some Web API calls
    # which return a list of dictionaries

mymod.pyi

def foo() -> list[Dict[AnyStr, AnyStr]]: ...

test1.py

import mymod

def main():
    events = mymod.foo()
    print(events)

test2.py

import mymod

def main():
    events = mymod.foo()
    for event in events:
        print(event)

test3.py

import mymod

def main():
    text = []
    events = mymod.foo()
    for event in events:
        params = event['text'].lower().split()
        for p in params:
            text.append(p)
        print(text)
        print(" ".join(text))

stubs/taint/general.pysa

# Sources
def mymod.foo() -> TaintSource[UserSpecified]: ...

# Sinks
def print(*values: TaintSink[RemoteCodeExecution], sep = ..., end = ..., file = ..., flush = ...): ...

stubs/taint/taint.config

{
  sources: [
    {
      name: "UserSpecified",
      comment: "use to annotate user input"
    }
  ],

  sinks: [
    {
      name: "RemoteCodeExecution",
      comment: "use to annotate execution of code"
    }
  ],

  features: [],

  rules: [
    {
      name: "Possible shell injection",
      code: 5001,
      sources: [ "UserSpecified" ],
      sinks: [ "RemoteCodeExecution" ],
      message_format: "Data from [{$sources}] source(s) may reach [{$sinks}] sink(s)"
    }
  ]
}

Directly using the tainted list (in test1.py) and elements of the tainted list (in test2.py) resulted in taint violations caught by pyre analyze. However, it was not able to detect potential problems in test3.py, where elements of a tainted list is used to construct a different list, which is later used by a sink. I'd think the list text in test3.py should also be tainted?

1one-w01f avatar May 02 '19 03:05 1one-w01f

Thanks for the report. I'll take a look at this (might take me until next week though since we'll be at PyCon).

dkgi avatar May 02 '19 04:05 dkgi

I just pushed a fix for this yesterday in https://github.com/facebook/pyre-check/commit/6ae378f3a7a2547be609bbf923d1064a350e19e7 and https://github.com/facebook/pyre-check/commit/36d307b95fd425f35f395e64e685506d2cf217d3

fahndrich avatar May 04 '19 03:05 fahndrich

@fahndrich Thank you very much for the updates. I ran the latest commit 5ffdae1 on my toy example above, and it looks like Pyre still couldn't find problems in test3.py. I notice that now it says Found no definition for $toplevel (fun) before Fixpoint iterations. Did I miss something in my setup?

1one-w01f avatar May 04 '19 04:05 1one-w01f

Ah sorry, forgot to mention that you need a model for list.append. I didn't add anything by default. So somewhere in a .pysa file, add

# List propagation
def list.append(self, data: TaintInTaintOut[Updates[self]]): ...

fahndrich avatar May 04 '19 15:05 fahndrich

@dkgi might know more about the $toplevel missing warning.

fahndrich avatar May 04 '19 15:05 fahndrich

Ah sorry, forgot to mention that you need a model for list.append. I didn't add anything by default. So somewhere in a .pysa file, add

# List propagation
def list.append(self, data: TaintInTaintOut[Updates[self]]): ...

@fahndrich Thanks for the information. After adding the list.append model to general.pysa, Pyre is now able to find the problem of print(text) in test3.py.

However, if I comment out the line of print(text), Pyre doesn't seem to be able to find the line in test3.py that says print(" ".join(text)). Do I need to also add a model for string.join?

1one-w01f avatar May 04 '19 21:05 1one-w01f

Typically not. If the analysis does not have a model or the source for string.join, it should just propagate taint through. Not sure what is going on here. @dkgi has done some work in that area. Maybe he knows more.

You can try adding an explicit model for join in your .pysa files.

string.join(iterable: TaintInTaintOut)

It shouldn't be necessary though.

fahndrich avatar May 04 '19 23:05 fahndrich

I can reproduce this locally. Not entirely sure what's going on though. Will investigate.

dkgi avatar May 05 '19 20:05 dkgi

Okay, we have a fix. Should be released shortly.

fahndrich avatar May 06 '19 20:05 fahndrich

The "".join(tainted_list) should be fixed by https://github.com/facebook/pyre-check/commit/5d09325726c4cdc22b9d1554bf94fbae833b7366

fahndrich avatar May 07 '19 05:05 fahndrich