Skip to content

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

Open

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
    stdlibStandard Library Python modules in the Lib/ directory
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Nov 21, 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.14bugs and security fixes3.15bugs and security fixesOS-macstdlibStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions