Skip to content

import ctypes.util on macOS loads a library from the working directory #162362

Description

@maurycy

Bug report

On macOS, import ctypes.util calls CDLL(find_library("c")) to look up two dyld functions for dllist(). The lookup can resolve to a library in the cwd:

https://github.com/python/cpython/blob/v3.15.2/Lib/ctypes/util.py#L153

My concern is that an app that works in a directory supplied by someone else. In such case, it could load a native code with the app's privileges.

A simple repro:

2026-11-21T08:27:12.094373000+0100 maurycy@gimel /Users/maurycy/work/cpython (main e0d0f2e?) % cat > ctor.c <<'C'
#include <stdio.h>
__attribute__((constructor))
static void loaded(void) {
    fprintf(stderr, "code from the working directory ran\n");
}
C
cc -dynamiclib -o libc.dylib ctor.c
2026-11-21T08:27:12.689690000+0100 maurycy@gimel /Users/maurycy/work/cpython (main e0d0f2e?) % ./python.exe -I -c "import ctypes.util"        

code from the working directory ran

This is similarish to #101283, since 3.14

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bug
    on Nov 21, 2026
  2. added
    stdlib
    3.14
    3.15
    on Nov 21, 2026
  3. ZeroIntensity commented on Nov 22, 2026

    @ZeroIntensity
    Member

    If you think this is a security problem, please contact the PSRT so they can triage it. I'm not super comfortable changing this based on a gut feeling.

  4. added
    pending
    on Nov 22, 2026
  5. encukou commented on Nov 23, 2026

    @encukou
    Member

    There's a public issue open, so there's no need for secrecy now. It can be triaged publicly.

    This looks valid to me.
    Also, +1 getting rid of the soft-deprecated find_library (at least in main).

  6. ZeroIntensity commented on Nov 23, 2026

    @ZeroIntensity
    Member

    It's not that I want secrecy; it's that I don't feel qualified to evaluate the security implications here. I'll trust you that this is valid.

  7. removed
    pending
    on Nov 23, 2026
  8. added a commit that references this issue on Dec 1, 2026
  9. added 2 commits that reference this issue on Dec 1, 2026
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.143.15OS-macstdlibtopic-ctypestype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions