Python: Improve "bind all interfaces" query#21590
Conversation
Adds test cases from #21582 demonstrating false negatives: - Address stored in class attribute (`self.bind_addr`) - `os.environ.get` with insecure default value - `gevent.socket` (alternative socket module)
This takes care of most of the false negatives from the preceding commit. Additionally, we add models for some known wrappers of `socket.socket` from the `gevent` and `eventlet` packages.
51859b9 to
c0ce669
Compare
Now that we're using global data-flow, we might as well make use of the fact that we know where the source is.
Looking at the results of the the previous DCA run, there was a bunch of false positives where `bind` was being used with a `AF_UNIX` socket (a filesystem path encoded as a string), not a `(host, port)` tuple. These results should be excluded from the query, as they are not vulnerable. Ideally, we would just add `.TupleElement[0]` to the MaD sink, except we don't actually support this in Python MaD... So, instead I opted for a more low-tech solution: check that the argument in question flows from a tuple in the local scope. This eliminates a bunch of false positives on `python/cpython` leaving behind four true positive results.
There was a problem hiding this comment.
Pull request overview
Improves the Python py/bind-socket-all-network-interfaces query to reduce false negatives (per #21582) by switching to global data-flow/Models-as-Data, and updates its tests to use inline expectations.
Changes:
- Convert the query to a
path-problemusing global taint tracking and Models-as-Data sinks. - Add/extend Models-as-Data type/sink models for
socket.socket.bind, and wrapper socket implementations ingevent/eventlet. - Migrate the query test to inline expectations and add regression cases.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| shared/mad/codeql/mad/ModelValidation.qll | Allows the new sink kind used by the query’s Models-as-Data integration. |
| python/ql/src/Security/CVE-2018-1281/BindToAllInterfaces.ql | Updates the query to use global data-flow and emit paths. |
| python/ql/lib/semmle/python/frameworks/Stdlib.model.yml | Adds a bind() sink model and a socket.socket type model entry. |
| python/ql/lib/semmle/python/frameworks/Gevent.model.yml | Models gevent.socket.socket return type as socket.socket. |
| python/ql/lib/semmle/python/frameworks/Eventlet.model.yml | Models eventlet socket wrappers’ return type as socket.socket. |
| python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces_test.py | Converts to inline expectations and adds FN regression scenarios. |
| python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces.qlref | Enables inline expectation postprocessing for the test. |
| python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces.expected | Updates expected output for the new path-problem/inline expectations harness. |
| python/ql/src/change-notes/2026-03-26-improve-bind-all-interfaces-query.md | Documents the analysis change and new library recognition. |
python/ql/src/change-notes/2026-03-26-improve-bind-all-interfaces-query.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
owen-mc
left a comment
There was a problem hiding this comment.
I'm not very familiar with the python library (or any dynamic language) but this seems good to me. Have you considered running MRVA to see if it gets many new results, and DCA to check performance?
yoff
left a comment
There was a problem hiding this comment.
Nice, good use of type models. I like the rewrite to a path query as well, it is far more readable now :-)
I did a MRVA run of the top 1000 Python projects (well, top 997 -- I ran out of patience). The baseline run had 170 alerts, and the improved query has 212. However, many of these are in fact the same alert location, differing only in the flow path/source. Deduplicating and comparing the results, we lose 3 FPs, and gain 3 TPs, with everything else remaining essentially the same. I have started a final DCA run (including the inlining into Once that finishes (and assuming everything looks good), I'll proceed with the merge. |
|
DCA looks good. Merging. |
Addresses the false negatives reported in #21582.
Also converts the tests for that query to use inline expectations.