Title: Crash: marshal.loads SIGSEGV on self-referencing TYPE_TUPLE with FLAG_REF · Issue #148653 · python/cpython · GitHub
Open Graph Title: Crash: marshal.loads SIGSEGV on self-referencing TYPE_TUPLE with FLAG_REF · Issue #148653 · python/cpython
X Title: Crash: marshal.loads SIGSEGV on self-referencing TYPE_TUPLE with FLAG_REF · Issue #148653 · python/cpython
Description: Bug report Bug description: Summary marshal.loads() deterministically segfaults on a 16-byte structural payload containing a TYPE_TUPLE | FLAG_REF whose elements include a TYPE_REF back to the partial tuple itself. The root cause is that...
Open Graph Description: Bug report Bug description: Summary marshal.loads() deterministically segfaults on a 16-byte structural payload containing a TYPE_TUPLE | FLAG_REF whose elements include a TYPE_REF back to the part...
X Description: Bug report Bug description: Summary marshal.loads() deterministically segfaults on a 16-byte structural payload containing a TYPE_TUPLE | FLAG_REF whose elements include a TYPE_REF back to the part...
Opengraph URL: https://github.com/python/cpython/issues/148653
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Crash: marshal.loads SIGSEGV on self-referencing TYPE_TUPLE with FLAG_REF","articleBody":"# Bug report\n\n### Bug description:\n\n## Summary\n\n`marshal.loads()` deterministically segfaults on a 16-byte structural\npayload containing a `TYPE_TUPLE | FLAG_REF` whose elements include a\n`TYPE_REF` back to the partial tuple itself.\n\nThe root cause is that `R_REF()` registers the tuple in `p->refs`\nbefore its slots are populated. A nested `TYPE_REF` back-reference\nthen yields the partial tuple to a hashing site (`PySet_Add`), and\n`tuplehash` calls `PyObject_Hash(NULL)` on the unfilled slot.\n\nTYPE_FROZENSET, TYPE_CODE, and TYPE_SLICE already use the correct\ntwo-phase pattern (`r_ref_reserve` / `r_ref_insert`) that avoids this.\nTYPE_TUPLE, TYPE_LIST, TYPE_DICT, and TYPE_SET do not.\n\nOriginally filed as GHSA-m7gv-g5p9-9qqq. PSRT assessed this as outside\nthe security threat model since marshal.loads is documented as not secure\nagainst malicious data. Converting to a public bug per their guidance.\n\n## Reproducer\n\n```python\nimport marshal\nmarshal.loads(b'\\xa8\\x02\\x00\\x00\\x00N\u003c\\x01\\x00\\x00\\x00r\\x00\\x00\\x00\\x00')\n```\n\nByte stream:\n- `\\xa8` = TYPE_TUPLE | FLAG_REF\n- `\\x02\\x00\\x00\\x00` = n = 2\n- `N` = item[0] = TYPE_NONE\n- `\u003c\\x01\\x00\\x00\\x00` = item[1] = TYPE_SET, n = 1\n- `r\\x00\\x00\\x00\\x00` = element = TYPE_REF(0), the partial outer tuple\n\nExit code 139 (SIGSEGV). faulthandler stack:\n\n```\nPySet_Add -> set_add_entry -> PyObject_Hash -> tuplehash -> NULL deref\n```\n\n## Affected versions\n\nCrashes on every version I tested: 3.9, 3.10, 3.11, 3.12, 3.13, 3.14.\n\n## Behavioral change after fix\n\nWith the two-phase pattern, the `Py_None` placeholder in `p->refs`\nis detected by the existing TYPE_REF handler at marshal.c:1675:\n\n```c\nif (v == Py_None) {\n PyErr_SetString(PyExc_ValueError, \"bad marshal data (invalid reference)\");\n break;\n}\n```\n\nSo the fix changes the behavior from SIGSEGV to\n`ValueError: bad marshal data (invalid reference)`.\n\n## Suggested fix\n\nA fix with regression tests for tuple, list, set, and dict\nself-reference payloads is at #148652 \n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148652\n* gh-148698\n* gh-148700\n* gh-148711\n* gh-148713\n* gh-148725\n* gh-148727\n* gh-148728\n* gh-152321\n* gh-155903\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mjbommar","@type":"Person","name":"mjbommar"},"datePublished":"2026-04-16T16:46:43Z","url":"https://github.com/python/cpython/issues/148653"}
| 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:1654ce2e-c9c3-2450-a6e0-4bcb62c865e3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEAE:16E4ED:7E69F5:9EDD25:6B021082 |
| html-safe-nonce | 08aa81a2e80f0b5fc2ee9cf4694e73207347806b6d5dfaf708c2557dfb162e40 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUFFOjE2RTRFRDo3RTY5RjU6OUVERDI1OjZCMDIxMDgyIiwidmlzaXRvcl9pZCI6IjQ2ODQyNTI5MjQzNjc0NzQ4MTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 00f2c14ec5bd8be4913879a2a881e09bd37cc16e680673cc39214b6d9aaaedce |
| hovercard-subject-tag | issue:4277204633 |
| 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/148653/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b2ba64be1543ff79f7fe14bceb4dc21cdac7ec28299ba59b27f4a3567e7c643/python/cpython/issues/148653 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b2ba64be1543ff79f7fe14bceb4dc21cdac7ec28299ba59b27f4a3567e7c643/python/cpython/issues/148653 |
| og:image:alt | Bug report Bug description: Summary marshal.loads() deterministically segfaults on a 16-byte structural payload containing a TYPE_TUPLE | FLAG_REF whose elements include a TYPE_REF back to the part... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mjbommar |
| 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