From 5e4b2fc42f72644ec34f82da22927266fc472a4c Mon Sep 17 00:00:00 2001 From: Martijn Hoekstra Date: Thu, 3 Dec 2026 19:29:05 +0100 Subject: [PATCH] docker: one FIPS mode for the Go processes and the Rust binaries (#13296) * docker: one FIPS mode for the Go processes and the Rust binaries In the image, Go processes ran with fips140=on by default (#7590) while the Rust binaries, which take their mode from fips.mode alone, ran with unrestricted TLS; and WEED_FIPS_MODE=off or strict made every Go process refuse to start, since the entrypoint's fips140=on did not match. The entrypoint now states the image's mode once, as WEED_FIPS_MODE_DEFAULT=on, and gives Go processes the fips140 GODEBUG that matches WEED_FIPS_MODE, else that default. The Rust binaries use the default when neither WEED_FIPS_MODE nor fips.mode sets a mode; Go processes check their GODEBUG against it and refuse to start on a mismatch, naming WEED_FIPS_MODE as the way to pick another mode. Setting WEED_FIPS_MODE_DEFAULT empty leaves every process in its own default, as outside the image. Co-Authored-By: Claude Opus 5.5 * docker: take the image's FIPS mode from a fips140 the caller sets GODEBUG=fips140=off, the entrypoint's advice since #7590, made every Go process exit once the Rust binaries joined the image's mode. The entrypoint now maps the caller's fips140 to WEED_FIPS_MODE_DEFAULT, so Go and Rust run in that mode and the old advice keeps working. Co-Authored-By: Claude Opus 5.5 * docker: say fips.mode alone cannot change the image's FIPS mode Co-Authored-By: Claude Opus 5.5 * docker: read an empty fips140 as off and debug as on, as Go does Co-Authored-By: Claude Opus 5.5 * docker: warn when GODEBUG fips140 picks the image's FIPS mode The entrypoint keeps translating a caller's fips140 into the mode for the Rust binaries, so existing docker run setups keep starting, but logs that WEED_FIPS_MODE is the setting that also covers launches without the entrypoint. Co-Authored-By: Claude Opus 5.5 * docker: set the FIPS mode after dropping privileges Running as root, the entrypoint re-runs itself through su-exec, and the second pass took the fips140 the first pass had exported for the caller's own, warning on default starts and prepending fips140 twice. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- docker/Dockerfile.go_build | 4 +-- docker/entrypoint.sh | 39 ++++++++++++++++++------ seaweed-common/src/fips.rs | 46 ++++++++++++++++++++++++----- weed/command/scaffold/security.toml | 8 +++-- weed/util/fips140_mode.go | 44 +++++++++++++++++++++------ weed/util/fips140_mode_test.go | 35 ++++++++++++++++++++++ 6 files changed, 146 insertions(+), 30 deletions(-) diff --git a/docker/Dockerfile.go_build b/docker/Dockerfile.go_build index 1709b2ae35..3bff737bdb 100644 --- a/docker/Dockerfile.go_build +++ b/docker/Dockerfile.go_build @@ -93,8 +93,8 @@ RUN mkdir -p /etc/seaweedfs COPY --from=builder /go/src/github.com/seaweedfs/seaweedfs/docker/filer.toml /etc/seaweedfs/filer.toml COPY --from=builder /go/src/github.com/seaweedfs/seaweedfs/docker/entrypoint.sh /entrypoint.sh -# FIPS 140-3 mode is ON by default (Go 1.24+) -# To disable: docker run -e GODEBUG=fips140=off ... +# FIPS 140-3 mode is on by default for every process (entrypoint.sh) +# To disable: docker run -e WEED_FIPS_MODE=off ... # Install dependencies and create non-root user RUN apk upgrade --no-cache && \ diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index df9fc609db..10fb046e93 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,14 +1,5 @@ #!/bin/sh -# Enable FIPS 140-3 mode by default (Go 1.24+) -# To disable: docker run -e GODEBUG=fips140=off ... -# For strict mode: docker run -e GODEBUG=fips140=only ... -# The default goes first: Go applies the last of repeated GODEBUG settings, so -# the caller's fips140 value wins. -# Transparent huge pages are a host-wide setting this image cannot change; to -# turn them off for this process only, append GODEBUG=disablethp=1. -export GODEBUG="fips140=on${GODEBUG:+,$GODEBUG}" - # Fix permissions for mounted volumes # If /data is mounted from host, it might have different ownership # Fix this by ensuring seaweed user owns the directory @@ -49,6 +40,36 @@ if [ "$(id -u)" = "0" ]; then exec su-exec seaweed "$0" "$@" fi +# FIPS 140-3 mode is on by default, for the Go processes and the Rust +# binaries alike. To pick another mode: docker run -e WEED_FIPS_MODE=off (or +# strict). A fips.mode in security.toml alone cannot change it, since Go +# processes would still get this mode's fips140 and refuse to start. +# WEED_FIPS_MODE_DEFAULT is the mode when nothing else sets one; set it empty +# to leave every process in its own default (fips140=off for Go, off for Rust). +# A fips140 the caller puts in GODEBUG still picks the mode, for the Rust +# binaries as well, unless WEED_FIPS_MODE is set, but is deprecated and warns; +# a Go process refuses to start when its fips140 differs from WEED_FIPS_MODE or +# fips.mode. +# Transparent huge pages are a host-wide setting this image cannot change; to +# turn them off for this process only, append GODEBUG=disablethp=1. +export WEED_FIPS_MODE_DEFAULT="${WEED_FIPS_MODE_DEFAULT-on}" +godebug_fips140="$(printf '%s\n' "$GODEBUG" | tr ',' '\n' | sed -n 's/^fips140=/=/p' | tail -n 1)" +case "$godebug_fips140" in + =off | =) WEED_FIPS_MODE_DEFAULT=off ;; + =on | =debug) WEED_FIPS_MODE_DEFAULT=on ;; + =only) WEED_FIPS_MODE_DEFAULT=strict ;; +esac +if [ -n "$godebug_fips140" ] && [ -z "$WEED_FIPS_MODE" ]; then + echo "Warning: choosing the FIPS mode with GODEBUG=fips140 is deprecated; set WEED_FIPS_MODE=off, on or strict, which also covers the Rust binaries when they start without this entrypoint." >&2 +fi +case "${WEED_FIPS_MODE:-$WEED_FIPS_MODE_DEFAULT}" in + off) fips140=off ;; + on) fips140=on ;; + strict) fips140=only ;; + *) fips140= ;; +esac +export GODEBUG="${fips140:+fips140=$fips140}${fips140:+${GODEBUG:+,}}${GODEBUG}" + for f in /etc/resolv.conf /etc/hosts; do if [ -f "$f" ] && [ ! -r "$f" ]; then echo "Warning: $f is not readable by uid $(id -u), so host name lookups may fail. Make it world-readable (0644)." >&2 diff --git a/seaweed-common/src/fips.rs b/seaweed-common/src/fips.rs index 87ec3b8dc9..d955c00177 100644 --- a/seaweed-common/src/fips.rs +++ b/seaweed-common/src/fips.rs @@ -22,6 +22,10 @@ use std::sync::OnceLock; pub const SETTING: &str = "fips.mode"; /// The environment variable that overrides [`SETTING`]. pub const ENV: &str = "WEED_FIPS_MODE"; +/// The environment variable holding the mode that applies when neither +/// [`ENV`] nor [`SETTING`] sets one; the Docker image's entrypoint sets it to +/// the mode it runs every process in. +pub const DEFAULT_ENV: &str = "WEED_FIPS_MODE_DEFAULT"; /// The modes `fips.mode` names. Unset reads as [`FipsMode::Off`], which is /// how every Rust binary behaved before the setting existed. @@ -125,24 +129,30 @@ impl fmt::Display for Setting { /// Resolves the mode of a process whose own security.toml loader read /// `file_value` for `fips.mode` from `file` (`None` when there is no file or /// it does not set the key): `WEED_FIPS_MODE` when set and non-empty, else -/// `file_value`, else [`FipsMode::Off`]. An unknown value is an error naming -/// where it came from. +/// `file_value` when non-empty, else `WEED_FIPS_MODE_DEFAULT`, the mode the +/// Docker image's entrypoint states, else [`FipsMode::Off`]. An unknown value +/// is an error naming where it came from. pub fn resolve( file: Option<&Path>, file_value: Option<&str>, env: &dyn Fn(&str) -> Option, ) -> Result { - let from_env = env(ENV) - .map(|v| v.to_string_lossy().into_owned()) - .filter(|v| !v.is_empty()); + let from = |name: &'static str| { + env(name) + .map(|v| v.to_string_lossy().into_owned()) + .filter(|v| !v.is_empty()) + .map(|v| (name.to_string(), v)) + }; let from_file = || { let source = match file { Some(path) => format!("{}: {SETTING}", path.display()), None => SETTING.to_string(), }; - file_value.map(|value| (source, value.to_string())) + file_value + .filter(|value| !value.is_empty()) + .map(|value| (source, value.to_string())) }; - let Some((source, value)) = from_env.map(|v| (ENV.to_string(), v)).or_else(from_file) else { + let Some((source, value)) = from(ENV).or_else(from_file).or_else(|| from(DEFAULT_ENV)) else { return Ok(Setting { mode: FipsMode::Off, source: None, @@ -247,6 +257,28 @@ mod tests { ); } + #[test] + fn the_image_default_applies_only_when_nothing_else_sets_the_mode() { + let env = env_of(&[("WEED_FIPS_MODE_DEFAULT", "on")]); + let setting = resolve(Some(Path::new(FILE_PATH)), Some(""), &env).unwrap(); + assert_eq!(setting.mode, FipsMode::On); + assert_eq!(setting.to_string(), "FIPS mode on (WEED_FIPS_MODE_DEFAULT)"); + assert_eq!(resolve(None, None, &env).unwrap().mode, FipsMode::On); + assert_eq!( + resolve(Some(Path::new(FILE_PATH)), Some("strict"), &env) + .unwrap() + .mode, + FipsMode::Strict + ); + let both = env_of(&[("WEED_FIPS_MODE_DEFAULT", "on"), ("WEED_FIPS_MODE", "off")]); + assert_eq!(resolve(None, None, &both).unwrap().mode, FipsMode::Off); + let unknown = env_of(&[("WEED_FIPS_MODE_DEFAULT", "yes")]); + assert_eq!( + resolve(None, None, &unknown).unwrap_err().to_string(), + "WEED_FIPS_MODE_DEFAULT is \"yes\"; want off, on or strict" + ); + } + #[test] fn an_empty_environment_variable_counts_as_unset() { let env = env_of(&[("WEED_FIPS_MODE", "")]); diff --git a/weed/command/scaffold/security.toml b/weed/command/scaffold/security.toml index 2427f633ee..dd08b5292c 100644 --- a/weed/command/scaffold/security.toml +++ b/weed/command/scaffold/security.toml @@ -26,9 +26,11 @@ # A set env var wins over the file, so the file can stay empty (or absent). # The FIPS 140-3 mode of every process that reads this file: "off", "on" or -# "strict" (WEED_FIPS_MODE). Unset leaves each process as it is. Go processes -# take the mode from GODEBUG and refuse to start when it differs: "off" needs -# fips140=off, "on" needs fips140=on, "strict" needs fips140=only. The Rust +# "strict" (WEED_FIPS_MODE). Unset, WEED_FIPS_MODE_DEFAULT applies, which the +# Docker image's entrypoint sets to "on"; without it each process stays as it +# is. Go processes take the mode from GODEBUG and refuse to start when it +# differs: "off" needs fips140=off, "on" needs fips140=on, "strict" needs +# fips140=only. The Rust # volume server, workers and vector engine take it from here: under "on" and # "strict" their TLS offers and accepts only the FIPS 140-3 versions, cipher # suites, key exchange groups and signature schemes Go allows in that mode. diff --git a/weed/util/fips140_mode.go b/weed/util/fips140_mode.go index 021c53b5d1..b3b4aa9517 100644 --- a/weed/util/fips140_mode.go +++ b/weed/util/fips140_mode.go @@ -12,9 +12,12 @@ import ( // mode every SeaweedFS process of a deployment runs in. Go processes take the // mode from GODEBUG and check it against this setting; the Rust binaries take // it from this setting alone. FipsModeEnv sets it from the environment. +// FipsModeDefaultEnv holds the mode that applies when neither sets one; the +// Docker image's entrypoint sets it to the mode it runs every process in. const ( - FipsModeSetting = "fips.mode" - FipsModeEnv = "WEED_FIPS_MODE" + FipsModeSetting = "fips.mode" + FipsModeEnv = "WEED_FIPS_MODE" + FipsModeDefaultEnv = "WEED_FIPS_MODE_DEFAULT" ) // The values of fips.mode. An empty value leaves every process as it is. @@ -85,15 +88,38 @@ func FipsModeValue(source string, v interface{ Get(string) interface{} }) (strin } } -// RequireFipsMode exits when fips.mode, from the environment or the -// security.toml merged into the shared configuration, is not a string or -// fails CheckFipsMode, so a process never runs in a FIPS mode other than the -// one the deployment states. Call it after loading security.toml. +// resolveFipsMode returns the mode a deployment states for this process and +// where it came from: WEED_FIPS_MODE or fips.mode in v, read from file, else +// WEED_FIPS_MODE_DEFAULT. An empty mode means none of them sets one. +func resolveFipsMode(file string, v interface{ Get(string) interface{} }) (source, mode string, err error) { + source = fipsModeSource(file) + mode, err = FipsModeValue(source, v) + if err != nil || mode != "" { + return source, mode, err + } + return FipsModeDefaultEnv, os.Getenv(FipsModeDefaultEnv), nil +} + +// checkResolvedFipsMode is CheckFipsMode for a mode resolveFipsMode returned. +// A mismatch with the image default says how to pick another mode, since the +// GODEBUG the caller set cannot change what the Rust binaries run in. +func checkResolvedFipsMode(source, mode string) error { + err := CheckFipsMode(source, mode) + if err != nil && source == FipsModeDefaultEnv { + return fmt.Errorf("%w; to run in another mode, set %s (or %s) instead of the fips140 GODEBUG", err, FipsModeEnv, FipsModeSetting) + } + return err +} + +// RequireFipsMode exits when the FIPS mode the deployment states, from the +// environment, the security.toml merged into the shared configuration, or the +// image default, is not a string or fails CheckFipsMode, so a process never +// runs in a FIPS mode other than the one the deployment states. Call it after +// loading security.toml. func RequireFipsMode() { - source := fipsModeSource(securityConfigFile()) - mode, err := FipsModeValue(source, GetViper()) + source, mode, err := resolveFipsMode(securityConfigFile(), GetViper()) if err == nil { - err = CheckFipsMode(source, mode) + err = checkResolvedFipsMode(source, mode) } if err != nil { glog.Fatalf("%v", err) diff --git a/weed/util/fips140_mode_test.go b/weed/util/fips140_mode_test.go index f598a69412..957df625a2 100644 --- a/weed/util/fips140_mode_test.go +++ b/weed/util/fips140_mode_test.go @@ -144,3 +144,38 @@ func TestFipsModeIsReadFromTheEnvironment(t *testing.T) { t.Fatalf("got %v, want the %s value refused", err, FipsModeEnv) } } + +func TestFipsModeDefaultAppliesWhenNothingElseSetsTheMode(t *testing.T) { + t.Setenv(FipsModeEnv, "") + t.Setenv(FipsModeDefaultEnv, "on") + cases := []struct{ env, toml, wantSource, wantMode string }{ + {"", "", FipsModeDefaultEnv, "on"}, + {"", "[fips]\nmode = \"\"\n", FipsModeDefaultEnv, "on"}, + {"", "[fips]\nmode = \"strict\"\n", "security.toml: fips.mode", "strict"}, + {"off", "", FipsModeEnv, "off"}, + } + for _, tc := range cases { + t.Setenv(FipsModeEnv, tc.env) + source, mode, err := resolveFipsMode("security.toml", securityToml(t, tc.toml)) + if err != nil || source != tc.wantSource || mode != tc.wantMode { + t.Errorf("env %q, file %q: got %q from %q, %v; want %q from %q", tc.env, tc.toml, mode, source, err, tc.wantMode, tc.wantSource) + } + } + t.Setenv(FipsModeEnv, "") + t.Setenv(FipsModeDefaultEnv, "") + if source, mode, err := resolveFipsMode("security.toml", securityToml(t, "")); err != nil || mode != "" { + t.Errorf("an empty default: got %q from %q, %v; want no mode", mode, source, err) + } +} + +func TestFipsModeDefaultMismatchSaysHowToPickAnotherMode(t *testing.T) { + setFIPS140Godebug(t, "off") + err := checkResolvedFipsMode(FipsModeDefaultEnv, "on") + want := FipsModeDefaultEnv + ` is "on", which needs GODEBUG=fips140=on; this process runs with fips140=off; to run in another mode, set WEED_FIPS_MODE (or fips.mode) instead of the fips140 GODEBUG` + if err == nil || err.Error() != want { + t.Fatalf("got %v\nwant %s", err, want) + } + if err := checkResolvedFipsMode("security.toml: fips.mode", "on"); err == nil || strings.Contains(err.Error(), "instead of") { + t.Fatalf("a mismatch with the file should not carry the hint: %v", err) + } +}