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 — Double-free in Argument Clinic `str_converter` generated code · Issue #99240 · python/cpython

Double-free in Argument Clinic str_converter generated code #99240

New issue
Copy link
New issue
Copy link
Closed
Closed
Double-free in Argument Clinic str_converter generated code#99240
Copy link
Assignees
gpshead
Labels
topic-argument-clinictype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@colorfulappl
colorfulappl
opened on Nov 8, 2022
Issue body actions

Argument Clinic str_converter generate such code when encoding is set
(see function test_str_converter_encoding in file Lib/test/clinic.test):

    /* -- snip -- */
    if (!_PyArg_ParseStack(args, nargs, "esesetes#et#:test_str_converter_encoding",
        "idna", &a, "idna", &b, "idna", &c, "idna", &d, &d_length, "idna", &e, &e_length)) {
        goto exit;
    }
    return_value = test_str_converter_encoding_impl(module, a, b, c, d, d_length, e, e_length);

exit:
    /* Cleanup for a */
    if (a) {
       PyMem_FREE(a);
    }
    /* Cleanup for b */
    if (b) {
       PyMem_FREE(b);
    }
    /* Cleanup for c */
    if (c) {
       PyMem_FREE(c);
    }
    /* -- snip -- */

If parsing a successes, a will be assigned an address points to an allocated memory.
After that, if parsing b fails, the memory which a points to is freed by function _PyArg_ParseStack,
and _PyArg_ParseStack returns 0, then control flow goes to label "exit".
At this time, a is not NULL, so the memory it points to is freed again, which cause a double-free problem and a runtime crash.

This bug is found in #96178 "Argument Clinic functional test".

  • PR: gh-99240: Fix double-free bug in Argument Clinic str_converter generated code #99241
  • PR: gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing #99890
  • PR: [3.11] gh-99240: Fix double-free bug in Argument Clinic str_converter generated code (GH-99241) #100352
  • PR: [3.10] gh-99240: Fix double-free bug in Argument Clinic str_converter generated code (GH-99241) #100353
  • PR: [3.11] gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (GH-99890) #100385
  • PR: [3.10] gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (GH-99890) #100386

Activity

Next
  1. colorfulappl
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 8, 2022
  2. colorfulappl
    changed the title [-]Double-fre in Argument clinic `str_converter` generated code[/-] [+]Double-free in Argument clinic `str_converter` generated code[/+] on Nov 8, 2022
  3. colorfulappl
    changed the title [-]Double-free in Argument clinic `str_converter` generated code[/-] [+]Double-free in Argument Clinic `str_converter` generated code[/+] on Nov 8, 2022
  4. colorfulappl
    added a commit that references this issue on Nov 8, 2022

    Add PoC of pythonGH-99240

    2cc6c0a
  5. colorfulappl commented on Nov 8, 2022

    @colorfulappl
    colorfulappl
    on Nov 8, 2022
    ContributorAuthor
    More actions

    There are two ways to fix this bug,

    1. Avoid free any parsed arguments if an error occurred in function _PyArg_ParseStack,
      as gh-99240: Fix double-free bug in Argument Clinic str_converter generated code #99241 have done.
    2. If function _PyArg_ParseStack parses failed, assign all the parsed arguments to "NULL" after they are freed, this should be done in _PyArg_ParseStack.
  6. erlend-aasland
    added
    topic-argument-clinic
    on Nov 8, 2022
  7. gpshead
    self-assigned this
    on Nov 9, 2022
  8. miss-islington
    added a commit that references this issue on Nov 24, 2022

    gh-99240: Fix double-free bug in Argument Clinic str_converter genera…

    8dbe08e
  9. hauntsaninja commented on Nov 29, 2022

    @hauntsaninja
    hauntsaninja
    on Nov 29, 2022
    Contributor
    More actions

    Thanks for reporting and fixing, looks like this has been completed

  10. hauntsaninja
    closed this as completedon Nov 29, 2022
  11. erlend-aasland commented on Nov 29, 2022

    @erlend-aasland
    erlend-aasland
    on Nov 29, 2022
    Contributor
    More actions

    We should consider backporting this, IMO.

  12. colorfulappl commented on Nov 30, 2022

    @colorfulappl
    colorfulappl
    on Nov 30, 2022
    ContributorAuthor
    More actions

    We should consider backporting this, IMO.

    Should we backport #96002 before backporting this?

    And I made a new bug fix #99890 .

  13. hauntsaninja
    reopened this on Nov 30, 2022
  14. erlend-aasland commented on Nov 30, 2022

    @erlend-aasland
    erlend-aasland
    on Nov 30, 2022
    Contributor
    More actions

    Should we backport #96002 before backporting this?

    Yes, I think we should. Although that PR introduced a new C file, it expands the Argument Clinic coverage considerably, so I think it is definitely worth it.

    And I made a new bug fix #99890 .

    Great :)

  15. kumaraditya303
    added a commit that references this issue on Dec 17, 2022

    gh-99240: Reset pointer to NULL when the pointed memory is freed in a…

    a6f82f1
  16. 1 remaining item

  17. shihai1991
    added a commit that references this issue on Dec 18, 2022

    Merge remote-tracking branch 'origin/main' into bpo_34697

    2878571
  18. carljm
    added a commit that references this issue on Dec 19, 2022

    Merge branch 'main' into fastmock

    a9af20a
  19. colorfulappl
    added 2 commits that reference this issue on Dec 20, 2022

    [3.11] pythongh-99240: Fix double-free bug in Argument Clinic str_con…

    32f7fad

    [3.10] pythongh-99240: Fix double-free bug in Argument Clinic str_con…

    0c00e33
  20. kumaraditya303
    added 2 commits that reference this issue on Dec 20, 2022

    [3.11] gh-99240: Fix double-free bug in Argument Clinic str_converter…

    ba8e30c

    [3.10] gh-99240: Fix double-free bug in Argument Clinic str_converter…

    53063b7
  21. colorfulappl
    added 2 commits that reference this issue on Dec 21, 2022

    [3.11] pythongh-99240: Reset pointer to NULL when the pointed memory …

    62c4a95

    [3.10] pythongh-99240: Reset pointer to NULL when the pointed memory …

    e02b61b
  22. kumaraditya303
    added 2 commits that reference this issue on Dec 21, 2022

    [3.11] gh-99240: Reset pointer to NULL when the pointed memory is fre…

    bed1d14

    [3.10] gh-99240: Reset pointer to NULL when the pointed memory is fre…

    591365c
  23. kumaraditya303 commented on Dec 21, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 21, 2022
    Contributor
    More actions

    Thanks for working on this, all PRs have been merged.

    Reacted by Erlend E. Aasland
  24. kumaraditya303
    closed this as completedon Dec 21, 2022
  25. erlend-aasland commented on Dec 23, 2022

    @erlend-aasland
    erlend-aasland
    on Dec 23, 2022
    Contributor
    More actions

    Yes, thanks for all your good work on argument clinic, @colorfulappl! And thank you Kumar for landing these PRs; I've had a hard time keeping up with CPython dev lately.

    Reacted by Shantanu and Kumar Aditya
  26. rwgk
    added a commit that references this issue on Mar 11, 2023

    pythongh-99240: Reset pointer to NULL when the pointed memory is free…

    9aad32a
  27. edo38
    added a commit that references this issue on Apr 24, 2024

    gh-99240: Fix double-free bug in Argument Clinic str_converter genera…

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

Metadata

Metadata

Assignees

  • @gpshead
    gpshead

Labels

topic-argument-clinictype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

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.