This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Code objects can contain unmarshallable objects
Type: Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: DiddiLeija, FFY00, petr.viktorin, shihai1991
Priority: normal Keywords:

Created on 2021-09-07 13:57 by petr.viktorin, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
reproducer_replace.py petr.viktorin, 2021-09-07 13:57
reproducer_compileall.py petr.viktorin, 2021-09-07 13:57
Messages (3)
msg401277 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-09-07 13:57
The `replace` method of `code` allows setting e.g.
* co_filename to a subclass of str
* co_consts to an arbitrary tuple
and possibly more weird cases.

This makes code objects unmarshallable.

One way to create such a code object is to call `compileall.compile_file` with a str subclass as path. See the attached reproducers.

This hit pip, see: https://github.com/pypa/pip/pull/10358#issuecomment-914320728
msg401278 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-09-07 14:01
How to fix this? I guess:

* co_filename should be converted to an actual str, or reject subclasses with an exception
* for co_consts, the [marshal docs] could be updated to add code objects to "containers". The [code docs] already say co_consts "is a tuple containing the literals"; if someone's putting in non-literals they're voiding the warranty.

And so on for all other fields of code objects.

[marshal docs]: https://docs.python.org/3/library/marshal.html
[code docs]: https://docs.python.org/3.9/reference/datamodel.html#index-55
msg401281 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-09-07 14:10
See also bpo-42839
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89290
2021-09-07 17:23:45shihai1991setnosy: + shihai1991
2021-09-07 15:12:41DiddiLeijasetnosy: + DiddiLeija
2021-09-07 15:11:21FFY00setnosy: + FFY00
2021-09-07 14:10:13petr.viktorinsetmessages: + msg401281
2021-09-07 14:01:23petr.viktorinsetmessages: + msg401278
2021-09-07 13:57:30petr.viktorinsetfiles: + reproducer_compileall.py
2021-09-07 13:57:18petr.viktorincreate