Files
seaweedfs/weed/util/fips140_hash.go
T
9ffb5fe185 filer, s3, filer.sync: one digest per cluster, set by the filer's -digest and followed by its clients (#12913)
* util, filer: choose the digest at filer startup with -digest

NewMd5 and NewSha1 returned SHA-256 cut to size whenever the process ran
under GODEBUG=fips140=only, so the kind of digest a cluster stores followed
each process's own environment. They now follow a digest mode set once at
startup: util.SetDigest("md5"|"sha256"), or "" to keep following
fips140.Enforced(). The default is unchanged.

weed filer -digest, weed server -filer.digest and weed mini -filer.digest
set it; fips140=only refuses md5 at startup. The leveldb2/leveldb3
directory-key marker follows the mode, as do the places that compare a
NewMd5 digest with Content-MD5 or pass it on as one: S3 PutObject, filer
HTTP uploads, the Content-MD5 the filer returns, and the uploader's
Content-MD5 to volume servers. Places that verify Content-MD5 with a real
MD5 (volume servers, S3 request bodies and archive uploads) keep keying on
fips140.Enforced(), since they can check it whenever MD5 is available.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, command: report the filer's digest and make its clients use it

GetFilerConfigurationResponse carries the filer's digest mode in a new
field 16, digest. weed s3, mount, webdav, sftp, iam and filer.copy adopt
it before they serve or write, so a gateway started with a different
GODEBUG writes the same kind of ETag and chunk digest as the rest of the
cluster. A process under fips140=only refuses a filer that reports md5. A
filer that reports no digest predates the field; the client then keeps
its own mode and logs a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.sync, s3 sink: keep digests within one kind

filer.sync copies entry digests and ETags as they are, so syncing a filer
with digest sha256 into one with md5 left SHA-256 ETags in the md5
cluster. It now reads both filers' digests and refuses to start when they
differ, with a warning when either filer does not report one.

The S3 sink sent the entry digest as Content-MD5 unless its own process
ran under fips140=only. From a sha256 source into a target that checks
Content-MD5 every single-part upload failed with BadDigest and the backup
retried it forever. It now asks the source filer for its digest once and
forwards the entry digest only when that is md5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.meta.backup, filer.backup, filer.replicate: keep digests within one kind

filer.meta.backup opened its backup store in its own digest mode, so a copy
of a sha256 filer made by a process outside fips140=only got MD5 directory
keys and could not be opened as that filer's store. It now adopts the source
filer's digest before the store opens.

filer.backup and filer.replicate with a filer sink copied entry digests into
the target filer without comparing digests, as filer.sync did before. Both
now refuse, and exit, when the source and target filers report different
digests. replication.CheckDigestsMatch is the shared check; filer.sync uses
it too.

weed server and weed mini apply -filer.digest before they start the master
and volume server, so every part of the process hashes with one digest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, filer.sync: refuse a peer filer with another digest; exit on mismatch

Filers peering through the meta aggregator now compare digests. A peer with
a store of its own would replay entries holding the other kind of digest
into this filer's store, so its changes are refused and the reason logged
on every reconnect. A fresh filer bootstrapping from such a peer refuses the
copy, so a new filer whose digest differs from the existing filers' exits at
startup. A peer on the same store writes into it directly, and refusing its
stream would only starve this filer's subscribers, so that mismatch is
logged as an error and the stream kept.

filer.sync exits with status 1 when the two filers use different digests,
as filer.backup and filer.replicate do: it is a configuration error a
restart cannot fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: -digest help says a store keeps the digest it was created with

The help text said existing data keeps the digest it was written with,
which reads as if the flag could be changed on a store with data. Stores
that hash keys stop finding existing entries under another digest, so the
text now says to set it when the store is created, and that switching means
copying the metadata into a new store with fs.meta.save and fs.meta.load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, s3, replication, admin, iceberg: address review of the digest checks

- A filer checks its peers' digests on every start, not only at the fresh-store
  bootstrap: the store signature is written when the store opens, so a fresh
  filer refused at bootstrap restarted as an initialized store and served
  without its peers' files.
- Under digest sha256 with MD5 available (not fips140=only), a client's
  Content-MD5 is checked against a separate real MD5 of the body, in S3 object,
  part and directory-marker uploads and in filer HTTP uploads. It goes
  unchecked only when no MD5 can be computed.
- filer.backup and filer.replicate exit only on a DigestMismatchError; a filer
  that cannot be reached is retried, so a backup started before its target is
  up resumes once the target answers.
- The admin file browser and Iceberg compaction write chunks into a filer they
  do not follow at process level. They read that filer's digest and pass it to
  the chunk upload (ChunkedUploadOption.Digest), so their content and chunk
  digests match the filer's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, s3 sink: retire digest-refused peers; no Content-MD5 for an unknown digest

- A peer refused for its digest counts as unreachable: the check now runs
  before notePeerReachable and its error wraps errPeerUnreachable, so the
  subscribe loop retires the peer from the low watermarks after the usual
  grace instead of keeping its zero watermark, which held every aggregated
  subscriber at the settled horizon.
- When the source filer's digest cannot be read, the S3 sink no longer sends
  the entry digest as Content-MD5: it may be a SHA-256 digest the target
  would reject.
- TestCheckPeerDigest skips the cases that need MD5 locally under
  GODEBUG=fips140=only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg, util: tests for the per-filer digest

The Iceberg tests' fake filer serves GetFilerConfiguration, which uploads now
call to read the target filer's digest; it reports none, as a filer that
predates the setting does. TestNewContentMd5 computes a real MD5, so it skips
under GODEBUG=fips140=only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: bound each startup peer digest read with a timeout

CheckPeerDigests read each peer's configuration with the caller's context,
which at startup has no deadline, so a peer that accepted the connection but
never answered held the filer's startup. Each read now has its own
peerDigestReadTimeout (10s, inside the one-minute announce cap), after which
the peer is logged and skipped like an unreachable one.

setDigestForTest restores the previous digest setting instead of the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util, s3, filer: one-shot digests for data held in memory

Md5For hashes with md5.Sum or sha256.Sum256 instead of writing into a
hash.Hash, and the Content-MD5 checks of S3 directory markers and of filer
uploads saved inside the filer use BodyMd5Of on the bytes they already hold.
Streaming uploads keep their io.TeeReader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: hash Content-MD5 on the upload's digest hash, not on its body

teeContentMd5 wrapped the request body in a TeeReader before
uploadRequestToChunks. CodeQL's go/reflected-xss query follows the body
from r.Body through the chunk upload to upload_content.go's
w.Write(prepared.data), which it resolves to the response-writer
wrappers in s3vectors, s3tables, webdav and server/common.go. Those
flows exist on master from other sources (extract, tagging, the
multipart POST); the wrapped body put them through lines this PR
changed, so CodeQL reported six of them as new.

newUploadHash now builds the digest hash that uploadReaderToChunks and
the inline-append paths already tee the body into, and adds the
separate MD5 there; uploadBodyMd5 reads it back for the check. The body
reaches uploadRequestToChunks as on master. Behaviour is unchanged:
under digest sha256 with MD5 available, Content-MD5 is checked against
the body's MD5, including empty and inline appends.

Checked with CodeQL 2.27.6 (go-queries 1.6.16, ReflectedXss.ql) on this
tree: no flow of the nine reflected-XSS results passes a changed line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* replication: keep the ReadFilerSignature name and return the digest with it

Renaming it to ReadFilerSignatureAndDigest left its other callers with an
undefined function. Keep the name and add the digest to what it returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util: keep HashToInt32 off the digest mode

HashToInt32 names things that processes derive on their own, such as a
mount's local socket path and MQ partition key hashes. weed mount adopts
a -digest=sha256 filer's digest before it names its socket, while
weed shell mount.configure keeps md5 and dialed a different socket.
Follow GODEBUG only, as before the digest setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.meta.backup: wait for the source filer's digest instead of exiting

A backup with a saved offset used to start its stream loop and retry
until the source filer answered. Reading the digest first made it exit
when started before the filer. Retry the read; exit only when the
digest is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-24 20:17:26 -08:00

199 lines
5.2 KiB
Go

package util
import (
"crypto/fips140"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"sync/atomic"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
// Digest modes. The mode decides what NewMd5 and NewSha1 hash with, and so
// the kind of every ETag, chunk digest and hashed store key a process writes.
const (
DigestMd5 = "md5"
DigestSha256 = "sha256"
)
var fips140Enforced = fips140.Enforced
const (
digestFollowGodebug int32 = iota
digestMd5
digestSha256
)
var digestSetting atomic.Int32
// SetDigest sets this process's digest mode. An empty name follows GODEBUG:
// sha256 under fips140=only, md5 otherwise. fips140=only refuses md5, since
// MD5 panics there. Call it before serving: digests already written keep the
// mode they were written in.
func SetDigest(name string) error {
if err := CheckDigest(name); err != nil {
return err
}
switch name {
case "":
digestSetting.Store(digestFollowGodebug)
case DigestMd5:
digestSetting.Store(digestMd5)
case DigestSha256:
digestSetting.Store(digestSha256)
}
return nil
}
// DigestIsSha256 reports whether NewMd5 and NewSha1 return truncated SHA-256.
func DigestIsSha256() bool {
switch digestSetting.Load() {
case digestMd5:
return false
case digestSha256:
return true
}
return fips140Enforced()
}
// Digest names the digest mode in effect.
func Digest() string {
if DigestIsSha256() {
return DigestSha256
}
return DigestMd5
}
// AdoptFilerDigest sets this process's digest mode to the one filer reports,
// so every process writing into a cluster writes one kind of digest. A filer
// that reports none predates the setting; the process then keeps its own mode.
func AdoptFilerDigest(filer, digest string) error {
if digest == "" {
glog.Warningf("filer %s does not report its digest; writing %s digests as this process's GODEBUG implies", filer, Digest())
return nil
}
if err := SetDigest(digest); err != nil {
return fmt.Errorf("filer %s uses digest %s: %w", filer, digest, err)
}
return nil
}
// CheckDigest reports whether this process can hash with the named digest,
// "" meaning its own mode: fips140=only refuses md5, since MD5 panics there.
func CheckDigest(name string) error {
switch name {
case "", DigestSha256:
return nil
case DigestMd5:
if fips140Enforced() {
return fmt.Errorf("digest %s needs MD5, which GODEBUG=fips140=only refuses", DigestMd5)
}
return nil
}
return fmt.Errorf("unknown digest %q, want %s or %s", name, DigestMd5, DigestSha256)
}
// DigestIsSha256For is DigestIsSha256 for the named digest, "" meaning this
// process's mode. It serves writers that write into a filer other than the
// one whose digest this process adopted.
func DigestIsSha256For(name string) bool {
switch name {
case DigestMd5:
return false
case DigestSha256:
return true
}
return DigestIsSha256()
}
// NewMd5 returns an MD5 hash, or SHA-256 cut to MD5's size when the digest
// mode is sha256: digests keep their length but differ.
func NewMd5() hash.Hash {
return NewMd5For("")
}
// NewMd5For is NewMd5 for the named digest, "" meaning this process's mode.
// Check the name with CheckDigest first: md5 panics under fips140=only.
func NewMd5For(name string) hash.Hash {
if DigestIsSha256For(name) {
return NewTruncatedSha256(md5.Size)
}
return md5.New()
}
// Md5For is Md5 for the named digest, "" meaning this process's mode. Check
// the name with CheckDigest first: md5 panics under fips140=only.
func Md5For(name string, data []byte) []byte {
if DigestIsSha256For(name) {
sum := sha256.Sum256(data)
return sum[:md5.Size]
}
sum := md5.Sum(data)
return sum[:]
}
// BodyMd5Of is BodyMd5 for a body held in memory: the content digest when it
// is the MD5, else the body's MD5 when MD5 is available, else nil.
func BodyMd5Of(contentDigest, data []byte) []byte {
if !DigestIsSha256() {
return contentDigest
}
if fips140Enforced() {
return nil
}
sum := md5.Sum(data)
return sum[:]
}
// NewContentMd5 returns an MD5 hash for checking a client's Content-MD5 next
// to the content digest, when that digest is not itself an MD5. It returns
// nil when the content digest is the MD5 (digest md5) or MD5 is refused
// (fips140=only); BodyMd5 then gives the MD5 to check against, or none.
func NewContentMd5() hash.Hash {
if !DigestIsSha256() || fips140Enforced() {
return nil
}
return md5.New()
}
// BodyMd5 returns the body's MD5 for a Content-MD5 check: the hash from
// NewContentMd5 when there is one, else the content digest when that is an
// MD5. It returns nil when neither is an MD5 (fips140=only), and Content-MD5
// then goes unchecked.
func BodyMd5(contentDigest []byte, contentMd5Hash hash.Hash) []byte {
if contentMd5Hash != nil {
return contentMd5Hash.Sum(nil)
}
if !DigestIsSha256() {
return contentDigest
}
return nil
}
// NewSha1 is NewMd5 for SHA-1.
func NewSha1() hash.Hash {
if DigestIsSha256() {
return NewTruncatedSha256(sha1.Size)
}
return sha1.New()
}
// NewTruncatedSha256 returns SHA-256 keeping the first size bytes of the digest.
func NewTruncatedSha256(size int) hash.Hash {
return &truncatedHash{Hash: sha256.New(), size: size}
}
type truncatedHash struct {
hash.Hash
size int
}
func (h *truncatedHash) Size() int { return h.size }
func (h *truncatedHash) Sum(b []byte) []byte {
return append(b, h.Hash.Sum(nil)[:h.size]...)
}