Skip to content

FIPS Adaptations #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cpython-unix/build-cpython.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ sed "${sed_args[@]}" "s|/tools/host|${TOOLS_PATH}/host|g" ${TOOLS_PATH}/host/sha
# We force linking of external static libraries by removing the shared
# libraries. This is hacky. But we're building in a temporary container
# and it gets the job done.
find ${TOOLS_PATH}/deps -name '*.so*' -exec rm {} \;
# FIPS: In order to build FIPS compatible Python, we don't want to
# remove the shared libraries libssl and libcrypto
find ${TOOLS_PATH}/deps -name '*.so*' ! -name 'libssl.*' ! -name 'libcrypto.*' -exec rm {} \;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Important!


tar -xf Python-${PYTHON_VERSION}.tar.xz

Expand Down Expand Up @@ -389,7 +391,7 @@ CONFIGURE_FLAGS="
--build=${BUILD_TRIPLE}
--host=${TARGET_TRIPLE}
--prefix=/install
--with-openssl=${TOOLS_PATH}/deps
--with-builtin-hashlib-hashes=sha256,sha512
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Important!

--with-system-expat
--with-system-libmpdec
--without-ensurepip
Expand Down
4 changes: 3 additions & 1 deletion cpython-unix/build-openssl-3.0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ EXTRA_TARGET_CFLAGS=${EXTRA_TARGET_CFLAGS/\-arch x86_64/}

EXTRA_FLAGS="${EXTRA_FLAGS} ${EXTRA_TARGET_CFLAGS}"

# FIPS: Notice the 'fips' and 'shared' flags
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Important!

/usr/bin/perl ./Configure \
fips \
--prefix=/tools/deps \
--libdir=lib \
${OPENSSL_TARGET} \
no-legacy \
no-shared \
shared \
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no-tests \
${EXTRA_FLAGS}

Expand Down
34 changes: 17 additions & 17 deletions cpython-unix/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,18 @@ def simple_build(
if "static" in build_options:
env["STATIC"] = 1

add_target_env(env, host_platform, target_triple, build_env)

# FIPS: For openssl, build locally and not on container, so skip setting toolchain env
if entry in ("openssl-1.1", "openssl-3.0"):
settings = get_targets(TARGETS_CONFIG)[target_triple]
env["OPENSSL_TARGET"] = settings["openssl_target"]
else:
add_target_env(env, host_platform, target_triple, build_env)

build_env.run("build-%s.sh" % entry, environment=env)
build_env.run(
"build-%s.sh" % entry,
environment=env,
user=("build" if client is None else "root")
)

build_env.get_tools_archive(dest_archive, tools_path)

Expand All @@ -281,10 +286,7 @@ def build_binutils(client, image, host_platform):

add_env_common(env)

build_env.run(
"build-binutils.sh",
environment=env,
)
build_env.run("build-binutils.sh", user="root", environment=env)

build_env.get_tools_archive(
toolchain_archive_path("binutils", host_platform), "host"
Expand Down Expand Up @@ -366,7 +368,7 @@ def build_libedit(

add_target_env(env, host_platform, target_triple, build_env)

build_env.run("build-libedit.sh", environment=env)
build_env.run("build-libedit.sh", environment=env, user="root")
build_env.get_tools_archive(dest_archive, "deps")


Expand Down Expand Up @@ -408,7 +410,7 @@ def build_tix(

add_target_env(env, host_platform, target_triple, build_env)

build_env.run("build-tix.sh", environment=env)
build_env.run("build-tix.sh", environment=env, user="root")
build_env.get_tools_archive(dest_archive, "deps")


Expand Down Expand Up @@ -469,10 +471,7 @@ def build_cpython_host(
if meets_python_maximum_version(python_version, v):
env[f"PYTHON_MEETS_MAXIMUM_VERSION_{normal_version}"] = "1"

build_env.run(
"build-cpython-host.sh",
environment=env,
)
build_env.run("build-cpython-host.sh", environment=env, user="root")

build_env.get_tools_archive(dest_archive, "host")

Expand Down Expand Up @@ -846,7 +845,7 @@ def build_cpython(

add_target_env(env, host_platform, target_triple, build_env)

build_env.run("build-cpython.sh", environment=env)
build_env.run("build-cpython.sh", environment=env, user="root")

extension_module_loading = ["builtin"]
crt_features = []
Expand Down Expand Up @@ -1145,11 +1144,12 @@ def main():
"zlib",
):
tools_path = "host" if action in ("m4", "patchelf") else "deps"

# FIPS: Build ssl library locally
_client = client if 'ssl' not in action else None
simple_build(
settings,
client,
get_image(client, ROOT, BUILD, docker_image),
_client,
get_image(_client, ROOT, BUILD, docker_image),
action,
host_platform=host_platform,
target_triple=target_triple,
Expand Down
4 changes: 2 additions & 2 deletions pythonbuild/buildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def install_toolchain_archive(

p = build_dir / basename
self.copy_file(p)
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name])
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name], user="root")

def install_artifact_archive(
self, build_dir, package_name, target_triple, build_options
Expand All @@ -66,7 +66,7 @@ def install_artifact_archive(
p = build_dir / basename

self.copy_file(p)
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name])
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name], user="root")

def install_toolchain(
self,
Expand Down
14 changes: 9 additions & 5 deletions pythonbuild/cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,15 @@ def derive_setup_local(
enabled_extensions[name]["setup_line"] = name.encode("ascii")
continue

# Force static linking if we're doing a fully static build, otherwise,
# respect the `build-mode` falling back to `static` if not defined.
section = (
"static" if "static" in build_options else info.get("build-mode", "static")
)
# FIPS: Always shared
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Important!

if name in ("_ssl", "_hashlib"):
section = "shared"
else:
# Force static linking if we're doing a fully static build, otherwise,
# respect the `build-mode` falling back to `static` if not defined.
section = (
"static" if "static" in build_options else info.get("build-mode", "static")
)
enabled_extensions[name]["build-mode"] = section

# Presumably this means the extension comes from the distribution's
Expand Down
8 changes: 4 additions & 4 deletions pythonbuild/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@
# using the latest available.
# Remember to update OPENSSL_VERSION_INFO in verify_distribution.py whenever upgrading.
"openssl-3.0": {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Important!

"url": "https://www.openssl.org/source/openssl-3.0.16.tar.gz",
"size": 15334967,
"sha256": "57e03c50feab5d31b152af2b764f10379aecd8ee92f16c985983ce4a99f7ef86",
"version": "3.0.16",
"url": "https://www.openssl.org/source/openssl-3.0.2.tar.gz",
"size": 15038141,
"sha256": "98e91ccead4d4756ae3c9cde5e09191a8e586d9f4d50838e7ec09d6411dfdb63",
"version": "3.0.2",
"library_names": ["crypto", "ssl"],
"licenses": ["Apache-2.0"],
"license_file": "LICENSE.openssl-3.txt",
Expand Down