Skip to content

Unchecked signed integer overflow in PyLong_AsPid() #117021

Closed

Description

@serhiy-storchaka

If pid_t has the same size as int, PyLong_AsPid is defined as PyLong_AsLong. if the size of int is less than the size of long, there are values out of the C int range but in the C long range. Calling PyLong_AsPid() with such argument will not raise an exception, but casting the result out of the C int range to pid_t has undefined behavior.

Most non-Windows 64-bit platforms are affected.

The simplest solution is to define PyLong_AsPid as PyLong_AsInt. It only applicable in 3.13, because PyLong_AsInt is new in 3.13. In older versions there is _PyLong_AsInt, but it is declared in Include/cpython/longobject.h, not in Include/longobject.h.

cc @vstinner, @pitrou

Linked PRs

Activity

  1. pitrou commented on Mar 19, 2024

    @pitrou
    Member

    Not sure what you mean by "define" PyLong_AsPid, but if you mean #define, then I think a real function should be exposed instead, for the benefits of non-C/C++ users.

  2. serhiy-storchaka commented on Mar 19, 2024

    @serhiy-storchaka
    MemberAuthor

    Currently it is defined by #define.

  3. vstinner commented on Mar 19, 2024

    @vstinner
    Member

    I don't get thr context of this issue. Which functions are you modifying? In which code? Is it related to Argument Clinic?

  4. serhiy-storchaka commented on Mar 19, 2024

    @serhiy-storchaka
    MemberAuthor
    #define PyLong_AsPid PyLong_AsLong

    There are also other definitions in different branches, but they are okay.

    It could be defined as

    # if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
    #   define PyLong_AsPid PyLong_AsInt
    # else
    #   define PyLong_AsPid PyLong_AsLong
    # endif

    Still not good if Py_LIMITED_API < 0x030d0000, but it is the best that can be done with the limited C API.

    On other hand, the status of PyLong_AsPid is unclear. It is not documented, not even mentioned in changelogs, but de-facto it is always available, independently from the Py_LIMITED_API setting. It was added by @gpshead in fb94c5f. Perhaps it should be treated as a private C API, but its name is not underscored.

    Similar functions PyLong_FromSocket_t and PyLong_AsSocket_t were made public, but than were made private again.

  5. serhiy-storchaka commented on Mar 19, 2024

    @serhiy-storchaka
    MemberAuthor

    Opened a discussion about status of PyLong_FromPid and PyLong_AsPid: https://discuss.python.org/t/status-of-pylong-frompid-and-pylong-aspid/48988.

  6. added 6 commits that reference this issue on Mar 20, 2024
  7. added a commit that references this issue on Mar 21, 2024
  8. added a commit that references this issue on Mar 25, 2024
  9. added a commit that references this issue on Apr 17, 2024
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

    3.11only security fixes3.12only security fixes3.13only security fixestopic-C-API

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions