GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Safer data serialization with marshal module · Issue #113626 · python/cpython

Safer data serialization with marshal module #113626

New issue
Copy link
New issue
Copy link
Closed
Closed
Safer data serialization with marshal module#113626
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Jan 1, 2024
Issue body actions

Feature or enhancement

The main purpose of the marshal module -- serialization of precompiled module code objects. This requires support of code objects, strings for names, and other primitive Python types and simple collection types referred by the code object.

It allows to use it as more data generic serialization tool -- more limited than pickle, but less limited than JSON. The marshal module supports different versions of the format and backward compatible with all earlier versions. But only if the data does not contain code objects. The format of the code objects changed with every Python version, and this is not reflected in marshal format version. Loading marshal data created in different Python version has undefined behavior if the data contains a code object.

I propose to add a keyword-only parameter allow_code with default value True in marshal functions. Specifying allow_code=False forbid saving and loading code objects. It allows to be safer when you load external data and to guarantee that the output can be safely loaded in other Python.

Linked PRs

  • gh-113626: Add allow_code parameter in marshal functions #113648

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jan 1, 2024
  2. serhiy-storchaka
    added a commit that references this issue on Jan 2, 2024

    pythongh-113626: Add allow_code parameter in marshal functions

    56f9616
  3. vaishnavi192 commented on Jan 6, 2024

    @vaishnavi192
    vaishnavi192
    on Jan 6, 2024
    Contributor
    More actions

    is this issue solved now? @bedevere-bot

  4. gpshead
    changed the title [-]Safe data serialization with marshal module[/-] [+]Safer data serialization with marshal module[/+] on Jan 11, 2024
  5. gpshead commented on Jan 11, 2024

    @gpshead
    gpshead
    on Jan 11, 2024
    Member
    More actions

    I appreciate that in your PR you are not describing marshal as "safe" or fit for any particular purpose. ie: the big Warning in https://docs.python.org/3/library/marshal.html always applies.

    While I don't mind this feature, I wonder what the point is? Who is expected to use this and why?

    We cannot make promises about the marshal data format safety and capabilities. It was never designed for such use.

    ie: Anywhere someone should not use pickle, they should also not use marshal, even with allow_code=False. Given that, in what scenarios do you see this feature actually being used?

    The marshal code hasn't been written with security in mind, nor do we fuzz test it. Nor do we do meaningful interoperability testing across versions. Because that hasn't been its purpose.

  6. serhiy-storchaka commented on Jan 12, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Jan 12, 2024
    MemberAuthor
    More actions

    It is still not safe because you can still create a malicious marshal data that has exponentially large complexity of loading, and the unmarshaller is perhaps vulnerable to stack overflow. But you need to construct it specially, not using the marshal module.

    marshal is one of builtin data formats, it is often faster and more compact than pickle and JSON. It is safe if used with trusted input. It is used as a data format. If it was unsupported, we would not bother with versioning and backward compatibility. But if a code object is accidentally leaked in the marshalled data, unmarshalling it in other Python version can crash or corrupt the Python interpreter. This option will protect you from this kind of errors.

    Reacted by Gregory P. Smith
  7. serhiy-storchaka commented on Jan 12, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Jan 12, 2024
    MemberAuthor
    More actions

    An example of malicious marshal data:

    t = ()
    for i in range(100):
        t = (t, t)
    
    k = (1, 2)
    import marshal
    data = marshal.dumps([k, t, {k: t}])
    # replace the reference to k as the dict key with the reference to t
    data = data.replace(b'r\0', b'r\3')
    x = marshal.loads(data)  # hangs
  8. gpshead commented on Jan 13, 2024

    @gpshead
    gpshead
    on Jan 13, 2024
    Member
    More actions

    Thanks, that makes sense. I'm fine with adding this. I think our existing marshal documentation warns people well enough about its caveats.

  9. serhiy-storchaka
    added a commit that references this issue on Jan 16, 2024

    gh-113626: Add allow_code parameter in marshal functions (GH-113648)

    d2d8332
  10. serhiy-storchaka
    closed this as completedon Jan 16, 2024
  11. kulikjak
    added a commit that references this issue on Jan 22, 2024

    pythongh-113626: Add allow_code parameter in marshal functions (pytho…

    645eb95
  12. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-113626: Add allow_code parameter in marshal functions (pytho…

    a8e86ef
  13. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-113626: Add allow_code parameter in marshal functions (pytho…

    339cee2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.