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

Python: remove the imprecise container taint steps #17030

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

Conversation

yoff
Copy link
Contributor

@yoff yoff commented Jul 22, 2024

We used to have taint steps from any element of a collection to the entire collection (see here).
These are very imprecise, leading to false positives (e.g. seen here and here).
They are also at odds with how other languages treat collections, see our issue about this.

We wish to keep the semantics, that if a collection is tainted, then all elements are considered tainted. Therefor we now try to not taint collections, if we have precise information about which elements are tainted.
For a list, if an element is tainted, we do not know which one, so any read is potentially reading tainted information.
There is not much difference between the list having content and the list being tainted.
But for a dictionary, if an entry is tainted and we know which one, only reads of the appropriate key is reading tainted information. All other reads should ideally be considered safe (they used to not be). If we do not know that other keys are safe, e.g. if the collection came from an untrusted source, we can taint the collection itself, and all reads will be considered dangerous. So for collections with precise content, there is a big difference between having content and the collection being tainted.

Thus we wish to remove these imprecise taint steps for tuples and dictionaries, where we track content precisely (we keep them for lists and sets, where content is imprecise anyway).
This PR now seems to demonstrate that we can achieve this, although with some caveats:

  • we use implicit reads, which makes reasoning about use/use-flow and sinks a bit complicated
  • we need a solution to additional flow steps for conversions

The issue with conversions is as follows: We are moving away from tainting collections when only precise content is tainted. But some operations may read any of the collection elements, for instance decoders. A call like

tainted_obj = {"foo": TAINTED_STRING}
encoded = ujson.dumps(tainted_obj)

used to transfer taint from tainted_obj to encoded, via an additional taint step. But now there is no taint to transfer because tainted_obj itself is not tainted. Instead, it has to make a read step. Adding read steps is not trivial, though, the best mechanism we have is that of flow summaries, but it is awkward to use here, or two reasons:

  1. We do not actually collect decoder calls, but rather their input and output, whereas flow summaries are formulated in terms of calls and access paths.
  2. Non-monotonic recursion

We might also be able to get the converter reads as implicit reads, but there is currently no mechanism for doing that for all taint flow configurations at once.

@yoff yoff force-pushed the python/no-imprecise-container-step branch from 34e04ff to 07e3829 Compare July 24, 2024 15:34
Comment on lines +4848 to +4850
/**
* Flow summaries for string manipulation methods.
*/

Check warning

Code scanning / CodeQL

Class QLDoc style. Warning

The QLDoc for a class should start with 'A', 'An', or 'The'.
* global (inter-procedural) taint-tracking analyses.
*/
module TaintTracking {
import semmle.python.dataflow.new.internal.tainttracking1.TaintTrackingParameter::Public

Check warning

Code scanning / CodeQL

Redundant import Warning test

Redundant import, the module is already imported inside
semmle.python.dataflow.new.internal.tainttracking1.TaintTrackingImpl
.
yoff added 14 commits September 9, 2024 13:45
for collections where one could read out a different element due to precise content
- fixes fully/partial  SSRF confusion
Default implicit read steps changed the semantics of our taint tracking tests.
This resets that semantics.
We include two new annotations to allow testing with implicit reads,
as well as a consistency query to prevent spurious implicit read steps.
expect empty query predicates
@yoff yoff force-pushed the python/no-imprecise-container-step branch from efcb918 to 243f656 Compare September 9, 2024 13:02
Consider how to make this maintainable.
Could we explicitly disallow implicit reads at specific sinks instead of rebuilding the config without them?
This is probably what we want, if we can get support for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant