Skip to content

Reject wheels that require executable stacks - #732

Open
CAOShurong wants to merge 10 commits into
pypa:mainfrom
CAOShurong:codex/issue-634-executable-stack
Open

CAOShurong wants to merge 10 commits into
pypa:mainfrom
CAOShurong:codex/issue-634-executable-stack

Conversation

@CAOShurong

@CAOShurong CAOShurong commented Aug 29, 2026 •

Copy link
Copy Markdown

Fixes #634.

Cache the nullable PT_GNU_STACK requirement in DynamicExecutable/DynamicLibrary while reading the dependency tree. Stack-policy evaluation now uses that metadata instead of reopening dependency ELF files.

  • Explicit executable stacks are rejected on every libc and architecture.
  • Missing headers are permitted on musl. On glibc, they use the architecture default: executable on armv7l, i686, s390x and x86_64 (including its ISA variants), non-executable on the other supported architectures.
  • Android requires an explicit non-executable declaration, as suggested by mhsmith; this is a policy requirement, not a claim about Android loader enforcement.

The check covers ELF files in the wheel, including executables and orphan libraries. External libraries only constrain policies which would actually graft them; the no-graft path remains separate. Missing-header diagnostics and JSON null retain the distinction between an absent declaration and an explicit non-executable one; a rejected target policy reports true.

Validation of 530e43625ca55ad7984ad21eaf0574f6bd558c25 on Windows Python 3.13:

  • 148 focused tests pass repeatedly. Source and non-editable wheel unit runs each produce 400 passed / 21 failed; every one of the original 298 outcomes is unchanged, and all 123 additional cases pass.
  • 45 real ELF/wheel API and show cases cover seven glibc architectures, seven musl architectures and Android AArch64, with absent/RW/RWX headers. The x86_64 repair --patcher none entry now rejects the missing-header wheel which the submitted baseline allowed; explicit RW succeeds and RWX is rejected.
  • Committed sdist/wheel payloads, clean installation, dependency checks, Ruff and Linux-target mypy pass. The configured Windows mypy hook retains the unchanged libc.py os.confstr error.

These are static ELF analysis and metadata-repair checks. Linux dlopen, native patchelf repair, Android runtime enforcement, other Python/OS versions and physical multi-architecture execution were not run locally. Public CI and maintainer acceptance remain separate.

Provenance correction: OpenAI Codex performed the investigation, implementation and automated verification; this text is also AI-generated. No personal human review or production use is claimed. The previous body’s “I reviewed the resulting diff” sentence was unsupported by verified evidence; this update removes that wording.

mayeut
mayeut previously requested changes Aug 30, 2026

@mayeut mayeut left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR
Can you update one of the integration tests using build_numpy to check for failure before patching openblas ? (requires moving openblas patch outside build_numpy)

Comment thread src/auditwheel/show-schema.json Resolved
@CAOShurong
CAOShurong force-pushed the codex/issue-634-executable-stack branch 2 times, most recently from 2b65e8f to 85bff4b Compare August 30, 2026 10:37
@mayeut
mayeut dismissed their stale review September 5, 2026 08:12

outdated

@mayeut
mayeut requested a lite review from Copilot September 5, 2026 08:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The new executable-stack detection is correctly integrated into policy evaluation and is covered by focused unit/integration tests, with matching CLI and JSON schema updates.

Pull request overview

This PR adds detection of ELF objects that request an executable stack (via PT_GNU_STACK + execute flag), classifying affected wheels as Linux-only in show output and rejecting them during repair for manylinux policies (fixes #634).

Changes:

  • Add elf_has_executable_stack() in elfutils and plumb results through wheel_abi to produce an executable_stack_policy.
  • Update auditwheel show --json schema/output (version bumped to 2) and include a new executable_stack boolean plus a text warning.
  • Make auditwheel repair error out when targeting a stricter policy than allowed due to executable-stack requirements; add unit/integration coverage.
File summaries
File Description
src/auditwheel/elfutils.py Adds ELF PT_GNU_STACK executable-stack detection helper.
src/auditwheel/wheel_abi.py Records executable-stack findings and incorporates them into policy selection.
src/auditwheel/main_show.py Emits new JSON flag + text warning; bumps JSON version to 2.
src/auditwheel/show-schema.json Updates JSON schema to version 2 and adds executable_stack field.
src/auditwheel/main_repair.py Rejects repairs to stricter policies when executable stack is required.
tests/unit/test_elfutils.py Adds unit tests for executable-stack detection.
tests/unit/test_wheel_abi.py Updates unit test to stub executable-stack detection.
tests/unit/test_main_show.py Updates JSON version expectations and validates new JSON key.
tests/integration/test_manylinux.py Adjusts numpy integration flow to validate rejection then clearing executable stack.
tests/integration/test_bundled_wheels.py Adds a targeted repair rejection test for executable-stack wheels.
Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/auditwheel/wheel_abi.py Outdated
Comment on lines +150 to +151
if elf_has_executable_stack(elf):
executable_stack.append(fn)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only applies to elf files already present in the wheels and thus misses grafted libs which explains the failing tests.

@CAOShurong
CAOShurong force-pushed the codex/issue-634-executable-stack branch from 85bff4b to 06b62ac Compare September 16, 2026 07:17

@auvipy auvipy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.30%. Comparing base (7cec8ec) to head (34724d9).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #732      +/-   ##
==========================================
+ Coverage   96.08%   96.30%   +0.22%     
==========================================
  Files          23       23              
  Lines        2042     2086      +44     
  Branches      388      403      +15     
==========================================
+ Hits         1962     2009      +47     
+ Misses         46       44       -2     
+ Partials       34       33       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: CAOShurong <170531907+CAOShurong@users.noreply.github.com>

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elf_has_executable_stack() returns False when PT_GNU_STACK is missing. A missing marker is not the same as an explicit non-executable GNU_STACK; legacy/unknown ELF can still be treated as needing an executable stack. For a reject-on-exec-stack check, can the no-GNU_STACK case be handled as unknown/unsafe rather than safe, with a fixture for it?

@mayeut

mayeut commented Oct 3, 2026

Copy link
Copy Markdown
Member

For a reject-on-exec-stack check, can the no-GNU_STACK case be handled as unknown/unsafe rather than safe

What's the behaviour of recent distributions in this case ? I'm not sure we should be stricter than this.

@sylvesterkaczmarek

Copy link
Copy Markdown

I checked current glibc. Missing PT_GNU_STACK is still not universally equivalent to a non-executable stack: glibc falls back to the architecture ABI default when the header is absent, and the glibc 2.41 execstack work explicitly calls out DSOs without PT_GNU_STACK as using the default executable-stack behavior. That caused real dlopen compatibility failures and led to the execstack tunable added in 2025.

So I would avoid returning False for the missing-header case. A tri-state result seems cleaner: executable / non-executable / unknown. If auditwheel wants distro parity rather than conservative rejection, unknown does not necessarily have to fail repair, but it should not be classified as explicitly safe.

References: https://snapshots.sourceware.org/glibc/trunk/latest/manual/html_node/Dynamic-Linking-Tunables.html and https://sourceware.org/pipermail/glibc-bugs/2025-February/058936.html

Signed-off-by: CAOShurong <170531907+CAOShurong@users.noreply.github.com>
@CAOShurong

Copy link
Copy Markdown
Author

The missing-header follow-up is implemented in b1f4bc32d5d858fdb3d3fd7e415511737aba9054, following the tri-state suggestion: absent PT_GNU_STACK now returns None, produces an unknown warning and JSON null, and does not add a blanket repair rejection. Explicit executable-stack rejection, per-policy graft whitelists and the no-graft path are retained.

The glibc loader and execstack documentation support the target-dependent distinction; they do not establish a single recent-distribution runtime result. No distro/dlopen matrix was run locally.

Real bundled ELF regressions cover absent/RW/RWX on x86_64, i686, AArch64 and big-endian s390x. Related tests pass 67/67 repeatedly, including against the isolated installed wheel and built-sdist tests; one known Windows-path case is explicitly deselected. Full unit runs retain the same21 baseline Windows/environment failures (254/21 before,275/21 after). Installed real CLI checks cover JSON/text, successful --patcher none metadata repair and explicit-stack/missing-input rejection; native ELF patching remains untested. The body records the build/provenance, type/hooks results and limitations.

This revision and communication were generated with OpenAI Codex. The prior body's unsupported personal-review wording has been corrected; no human review or production use is claimed.

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked current head 0a7b9cc. The missing-PT_GNU_STACK case now has the tri-state behavior I asked for: explicit RW remains false, explicit RWX remains true, and an absent header is represented as unknown/None rather than safe. That state is surfaced in text/JSON and covered with real ELF fixtures across multiple architectures; grafted-library handling also treats unknown separately without converting it into a blanket repair rejection. My earlier concern is resolved. No remaining blocker from my review.

@mayeut mayeut left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up but digging a bit more into this leads to _get_executable_stack_policy needing to handle the libc and architecture specific behavior.

The behavior per libc is as follow:

  • musl: does not read PT_GNU_STACK for anything other than the stack size
  • android: if @mhsmith knows, I'd defer to his knowledge, I've not checked that one
  • glibc:
    • The following architectures default to executable stack given glibc source code (grep -r DEFAULT_STACK_PROT_PERMS glibc-2.44 | grep -v ChangeLog | sort):
      • armv7l
      • i686
      • s390x
      • x86_64
        I've checked that a missing PT_GNU_STACK does indeed trigger a failure to dlopen on x86_64 but not on aarch64
    • Regarding in which context we'd want to check this, it can become trickier if we don't apply some security rule on top of what exists for executables in Linux distros:
      • For python extensions and their dependencies, we do want the check (dlopen).
      • For executables and their dependencies, we don't want the check.
      • If there are some "orphan" (not a python extension, an executable or one of their dependencies) .so in the wheel, we'll want to check them as well (they'll most likely be loaded with dlopen).

I've said in an earlier comment that I did not think we should be stricter than Linux distros but I did not think of the differences between python extensions, executables, "orphan" so. This is just adding too much complexity here and in this case, I'd rather be stricter:

  • reject when explicitly executable on all libc / architecture
  • on musl, ignore missing PT_GNU_STACK
  • on glibc, missing PT_GNU_STACK should be handled per architecture to get the correct default value.

I missed that _get_executable_stack_policy re-opens ELF files to parse them again. This will be viewed as a regression speed-wise. It might be better to move elf_has_executable_stack in lddtree.py and add the relevant member to DynamicExecutable/DynamicLibrary.

@mhsmith

mhsmith commented Oct 11, 2026

Copy link
Copy Markdown
Member

I can see that the Android linker defaults to producing files with an PT_GNU_STACK header marked as non-executable. I don't know how strictly enforced this is at runtime, but given Android's increasingly tight attitude to security, I think auditwheel requiring an explicitly non-executable stack would be a good idea.

@CAOShurong

Copy link
Copy Markdown
Author

Updated in 530e436: the stack requirement is now cached in the dependency tree, so policy evaluation no longer reopens ELF files. Missing headers follow the glibc architecture defaults, are allowed on musl, and require an explicit non-executable declaration on Android. Explicit executable requests remain rejected everywhere, including executables/orphan libraries; policy-specific grafting and no-graft handling are preserved.

The installed x86_64 repair entry now rejects the missing-header case. Repeated unit runs retain the same 21 Windows baseline failures, with all 123 additional cases passing. Linux loader, native patchelf and Android runtime checks remain untested locally.

@mayeut
mayeut self-requested a review November 8, 2026 07:45

@mayeut mayeut left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@mayeut
mayeut requested a review from lkollar November 14, 2026 07:16
Comment thread src/auditwheel/wheel_abi.py Outdated
log.debug("processing: %s", fn)
elftree = ldd(fn, exclude=exclude, ldpaths=ld_paths_from_arg(args_ldpaths))

if elftree.executable_stack is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this check after the architecture and libc checks? With the current order we could end up in a situation when an unrelated .so with incompatible architecture causes the wheel to fail the executable stack check.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @lkollar. I checked get_wheel_elfdata(): it adds the ELF to executable_stack / unknown_executable_stack before the architecture and libc checks can skip that file, so an incompatible .so can still affect the stack policy. Moving the collection after those checks makes sense. A regression with a foreign-architecture library that requests an executable stack would pin this down. Thanks for spotting it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved stack collection after the architecture and libc checks. The real-wheel regressions cover foreign/unknown architectures and incompatible libc, with missing, RW and RWX stack headers; compatible unsafe libraries remain rejected. All 24 new cases pass, including from the built wheel. Existing unit/bundled-wheel outcomes are unchanged on Windows (422 passed, 27 pre-existing platform failures).

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auditwheel shall check & reject shared objects with executable stack

7 participants