Python Wheels Inherit Their Build Image's Hardening

· 6 min read · Mark Esler

The same C source gets a stack canary (a guard value checked before a function returns) or not, depending on the image that builds it. chard 0.2.1 read what gcc and clang apply at -O2 with no other flags on 2026-11-29 across 414 configurations: distributions’ own images, containers, Oracle Cloud images, GitHub’s runners, and the build images (manylinux, CI vendors’ images). Code You Build Yourself Gets the Compiler’s Defaults, Not the Distribution’s Flags has the method and what four distributions’ package builds pass beside it.

The build images

Of 37 build-image configurations (one per image, architecture and build invocation), 24 emit no stack canary. The manylinux images (2014, 2_28 and 2_34, on x86_64 and aarch64) apply no stack canary, no _FORTIFY_SOURCE (bounds-checked libc calls; chard reads it unsupported on 2014 and AWS CodeBuild) and no stack-clash protection, and get only partial RELRO (lazy-binding GOT entries stay writable), with or without CPython’s recorded flags. The same holds for the compilers in Docker’s official gcc images, GitLab.com’s default ruby:3.1 CI image and AWS CodeBuild’s standard:6.0 and standard:4.0 images.

Four images apply a canary, _FORTIFY_SOURCE and stack-clash protection: CircleCI’s cimg/base, Heroku’s build image, Google’s buildpacks builder and GitHub’s ubuntu-slim. All four ship Ubuntu 24.04’s gcc 13.3.0, whose defaults turn these on. Their shared objects, which is what a wheel holds, still get partial RELRO. On GitHub’s hosted Ubuntu runners gcc and clang differ: gcc applies the canary and stack-clash protection on every runner; clang applies no canary, and no stack-clash protection on the five runners where chard reads it. PyPA’s musllinux_1_2 images, on Alpine’s gcc, emit a canary and full RELRO, shared objects included.

The wheels of the 100 most-downloaded PyPI projects that ship a compiled manylinux x86_64 wheel were read with readelf on 2026-11-28; 76 of them also ship a musllinux_1_2 wheel of the same release.

Projects among the 100manylinux wheelmusllinux wheel
ship the wheel10076
with non-Rust compiled files of their own7961
full RELRO in all of those files1 of 7961 of 61
a stack canary in at least one13 of 7960 of 61

The rest ship only Rust files, which read full RELRO on both images and import no stack canary; only RELRO and the canary were read. The canary row is a lower bound: a file with no array or address-taken local on its stack imports none. chard did not measure a flag’s cost. The current reading for every image is at chard.page.

The images inherit their compilers. manylinux2014 is based on CentOS 7 and manylinux_2_28 and 2_34 on AlmaLinux 8 and 9, with Red Hat’s toolset compilers; Enterprise Linux hardens its own packages through the package build, so its compiler alone applies no canary, no fortify, no stack-clash protection and only partial RELRO. musllinux is based on Alpine, whose gcc applies a canary and full RELRO itself. Apart from Rust, the wheels mostly carry what the image’s upstream compiler applies: 1 of 79 projects’ manylinux wheels has full RELRO in its non-Rust files, against 61 of 61 on musllinux.

Proof of concept: one wheel, two builds

The extension pocdemo has three deliberate bugs, each tied to one flag: a stack buffer overflow (strcpy into a 16-byte buffer), an oversized copy into a known-size buffer (memcpy), and a read of an uninitialized stack variable (one call plants 42 on the stack, the next reads it back). It was built as a manylinux wheel twice (cibuildwheel, the manylinux_2_34 image, setuptools, auditwheel repair), changing nothing but the flags passed through CIBW_ENVIRONMENT. The PoC is not published. chard reads what ended up in each binary:

canaryfortifyRELROCET (control-flow marking, -fcf-protection)
defaultnononepartialno
with six of the OpenSSF guide’s flagsyes__memcpy_chk, __strcpy_chkfullyes

Installed in a clean CPython 3.13 and called the same way:

calldefault wheelhardened wheel
pocdemo.overflow(b"B"*64)segfault; gdb shows the return address overwritten with 0x4242424242424242*** buffer overflow detected ***, aborted
pocdemo.fortify_copy(64)segfault*** buffer overflow detected ***, aborted
pocdemo.uninit()42, left by the previous call0

Only the flags differ, and they are off by default. With _FORTIFY_SOURCE=3 on, fortify aborts the two overflows at the copy, before the function returns.

Impact

Scope. Defaults being off does not show a wheel is exploitable: that takes a memory-safety bug in the extension and input that reaches it. What the defaults decide is how much a bug, if one exists, gives an attacker. The PoC’s bugs are planted.

The fix

A request that the manylinux images enable hardening by default, pypa/manylinux#59, has been open since 2016-04-28. A project can opt in. On x86_64 manylinux_2_28 and 2_34 (the PoC used 2_34), a project can set six of the OpenSSF guide’s flags through cibuildwheel:

CIBW_ENVIRONMENT_LINUX='CFLAGS="-O3 -DNDEBUG -fno-strict-overflow -fstack-protector-strong -D_FORTIFY_SOURCE=3 -fstack-clash-protection -fcf-protection=full -ftrivial-auto-var-init=zero" LDFLAGS="-Wl,-z,relro,-z,now"'

Setting CFLAGS replaces CPython’s own, so the recipe repeats the ones that change the code (-O3 -DNDEBUG -fno-strict-overflow); add back anything else the project relies on. On aarch64, use -mbranch-protection=standard for -fcf-protection=full; manylinux2014’s gcc 10 has no -ftrivial-auto-var-init. auditwheel repair keeps your objects’ flags, so the repaired wheel is the hardened wheel in the table.

A default in the manylinux images’ gcc reaches every wheel built on them; a protection in the flags recorded by their CPython builds reaches every build that sets no CFLAGS of its own. Either shows in the image’s rows on chard.page.

Check your own

Look up your CI or wheel image on chard.page, or run chard on it. If it emits no canary, set the flags above (and CXXFLAGS for C++); the OpenSSF guide lists the rest, with the compiler version each needs. Check that the repaired wheel’s .so imports __stack_chk_fail (not every extension does).


Every musllinux wheel read here has full RELRO; Alpine’s gcc applies it with no flag. The companion measurement reads what each distribution’s compiler applies on its own.