Title: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) · Issue #149474 · python/cpython · GitHub
Open Graph Title: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) · Issue #149474 · python/cpython
X Title: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) · Issue #149474 · python/cpython
Description: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) Affects only main / 3.15-dev — not a PSRT case Per python.org/dev/security/, the PSRT does not accept reports that only affect pre-release versions. The _remote_de...
Open Graph Description: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) Affects only main / 3.15-dev — not a PSRT case Per python.org/dev/security/, the PSRT does not accept reports that only aff...
X Description: _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) Affects only main / 3.15-dev — not a PSRT case Per python.org/dev/security/, the PSRT does not accept reports that only aff...
Opengraph URL: https://github.com/python/cpython/issues/149474
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578)","articleBody":"# `_remote_debugging.BinaryWriter()` bypasses file-open audit hooks (PEP 578)\n\n## Affects only `main` / 3.15-dev — not a PSRT case\n\nPer [python.org/dev/security/](https://www.python.org/dev/security/), the PSRT does not accept reports that only affect pre-release versions. The `_remote_debugging.BinaryWriter` API is new in 3.15-dev and does not exist on any supported branch, so this is a regular bug report rather than a private security advisory.\n\n| Branch | `Modules/_remote_debugging/binary_io_writer.c` exists? | Affected? |\n|---|---|---|\n| main (3.15-dev) | yes | **yes** |\n| 3.14, 3.13, 3.12, 3.11, 3.10 | no | no |\n\n(The whole `_remote_debugging.BinaryWriter` API was added in 3.15-dev as part of the new `profiling.sampling` binary-replay workflow.)\n\n## What's wrong\n\n`Modules/_remote_debugging/binary_io_writer.c::binary_writer_create` calls `fopen(filename, \"wb\")` to open the output file, but does **not** fire `PySys_Audit(\"open\", \"sCi\", filename, 'w', 0)` first. The canonical pattern in cpython is to fire the audit hook before `fopen`:\n\n- `Modules/readline.c:347` — fires `PySys_Audit(\"open\", \"sCi\", filename, 'w', 0)` before `fopen`.\n- `Modules/_io/fileio.c::_io_FileIO___init___impl` — fires `PySys_Audit(\"open\", \"OOi\", ...)` before opening the FD.\n\n`binary_writer_create` follows neither precedent. An operator's `open` audit-hook does not fire when `_remote_debugging.BinaryWriter(target)` writes to `target`, even though the operator's same hook DOES fire on `builtins.open(target, \"w\")` and `_io.FileIO(target, \"w\")`.\n\n## Reproducer\n\nOn a fresh `main` checkout (a debug build is fine, e.g. `./configure --with-pydebug && make -j8`):\n\n```python\nimport os, sys, _remote_debugging\n\nevents = []\ndef hook(event, args):\n if event == \"open\":\n events.append({\"path\": str(args[0]) if args else None,\n \"mode\": str(args[1]) if len(args) > 1 else None})\n\nTARGET = \"/tmp/poc-binary-writer.prof\"\nCONTROL = \"/tmp/poc-builtins-open.txt\"\nfor p in (TARGET, CONTROL):\n if os.path.exists(p): os.unlink(p)\n\nsys.addaudithook(hook)\n\n# Negative control — should fire the audit hook (built-in `open`):\nwith open(CONTROL, \"w\") as f:\n f.write(\"control\\n\")\n\n# Target — should fire the audit hook per PEP 578, currently does NOT:\nCOMP_NONE = getattr(_remote_debugging, \"COMPRESSION_NONE\", 0)\nw = _remote_debugging.BinaryWriter(TARGET, sample_interval_us=1000,\n compression=COMP_NONE, start_time_us=0)\nw.close()\n\ncontrol_fired = any(e[\"path\"] == CONTROL for e in events)\ntarget_fired = any(e[\"path\"] == TARGET for e in events)\nprint(f\"control_fired={control_fired} target_fired={target_fired}\")\n# Expected (post-fix): control_fired=True target_fired=True\n# Actual on baseline: control_fired=True target_fired=False\n\nfor p in (TARGET, CONTROL):\n if os.path.exists(p): os.unlink(p)\n```\n\n## Suggested fix\n\nInsert the canonical `PySys_Audit` call before the `fopen`. Pattern matches `Modules/readline.c:347` verbatim.\n\n```diff\ndiff --git a/Modules/_remote_debugging/binary_io_writer.c b/Modules/_remote_debugging/binary_io_writer.c\n@@ -802,6 +802,10 @@ binary_writer_create(const char *filename, uint64_t sample_interval_us, int comp\n }\n }\n\n+ if (PySys_Audit(\"open\", \"sCi\", filename, 'w', 0) \u003c 0) {\n+ goto error;\n+ }\n+\n writer->fp = fopen(filename, \"wb\");\n if (!writer->fp) {\n PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);\n```\n\n## Empirical verification\n\nPatched against fork SHA `836fbdaa` (close to upstream `main`):\n\n| Run | `control_fired` (`builtins.open(CONTROL)`) | `target_fired` (`BinaryWriter(TARGET)`) |\n|---|---|---|\n| Baseline | true | **false** (vuln) |\n| Patched | true | true |\n\nNegative control fires correctly on both trees; mutation-confirmed sound (reverting just the patched line returns `target_fired=false`).\n\nPoC soundness verdict: `sound`.\n\n## Severity\n\nPEP 578 contract violation (audit-hook visibility gap on a documented profiling tool's file-write path), not RCE. An attacker who can invoke `_remote_debugging.BinaryWriter` writes to arbitrary paths without operator hook visibility. Recommend `topic-security` / `topic-audit-hooks` triage labels.\n\n## Notes for triage\n\n- 4-line idiomatic insertion that matches the canonical PEP 578 implementation pattern in cpython (`Modules/readline.c:347`).\n- No public-API surface change: `_remote_debugging.BinaryWriter()` constructor signature, return type, and exception types are unchanged.\n- Operators who reject the event via hook (raise an exception) will now see `BinaryWriter()` raise — aligns with all other audited file-open call sites.\n\nThis issue is part of a small set of PEP 578 audit-hook bypass findings observed on `main`; a sibling issue covers `posix._clearenv()` / `os.environ.clear()` (file `Modules/posixmodule.c`).\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149524\n* gh-149586\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/omkhar","@type":"Person","name":"omkhar"},"datePublished":"2026-05-06T21:47:56Z","url":"https://github.com/python/cpython/issues/149474"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:303417c3-cb33-532a-87c0-641baba89855 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94AE:1CCECA:117813D:174F4C0:6B01F345 |
| html-safe-nonce | 35accff756e77e4a6e5dbdec5765d43304af7001d7ee0e9c5e86dce48dae4979 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEFFOjFDQ0VDQToxMTc4MTNEOjE3NEY0QzA6NkIwMUYzNDUiLCJ2aXNpdG9yX2lkIjoiNDc4NzM3MjE4NTc0NjUzNTIzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f5074b860307b21536cf4bdfccd39ca6c544e3e03d7d53eff728101625e92fab |
| hovercard-subject-tag | issue:4394657747 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149474/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70ef64726669ef7bccacc2e11f90a59e4dcf47983cf434438de5ae39681cee7d/python/cpython/issues/149474 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70ef64726669ef7bccacc2e11f90a59e4dcf47983cf434438de5ae39681cee7d/python/cpython/issues/149474 |
| og:image:alt | _remote_debugging.BinaryWriter() bypasses file-open audit hooks (PEP 578) Affects only main / 3.15-dev — not a PSRT case Per python.org/dev/security/, the PSRT does not accept reports that only aff... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | omkhar |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width