Files
seaweedfs/weed/mount/locked_entry.go
T
Chris LuandClaude Opus 5.5 d0661afad8 mount: refuse SSE-KMS and SSE-C objects the native read path cannot decrypt (#13027)
* mount: refuse SSE-KMS and SSE-C objects the native read path cannot decrypt

The native read check only recognized SSE-S3, so objects the S3 gateway
stored with SSE-KMS or SSE-C were read chunk by chunk and the mount
returned their ciphertext with success. Detect every S3 server-side
encryption type, from the object metadata or any chunk, and answer
open, read and copy_file_range with ENOTSUP unless the object is SSE-S3
and -sseS3Read routes it through a gateway.

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

* mount: drop S3 encryption markers when a file is truncated to zero

A full truncate removes the encrypted payload, but the object metadata
still named SSE-C, SSE-KMS or SSE-S3, so the plaintext the mount wrote
afterwards was refused on read. Clear those markers in SetAttr and
truncateEntry when the size becomes zero, and reset the handle's cached
classification.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 08:41:20 -08:00

102 lines
2.8 KiB
Go

package mount
import (
"sync"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
)
type LockedEntry struct {
*filer_pb.Entry
sync.RWMutex
sseCheck sync.Once
sse filer_pb.SSEType
}
// Native reads cannot decrypt S3 objects. Check the entry once per metadata
// change so reads of ordinary files do not repeatedly scan their chunk list.
// Any type other than SSE-S3 wins, since no mount option can read it.
func (le *LockedEntry) sseType() filer_pb.SSEType {
le.RLock()
defer le.RUnlock()
return le.sseTypeLocked()
}
func (le *LockedEntry) sseTypeLocked() filer_pb.SSEType {
le.sseCheck.Do(func() {
metadata := le.Entry.GetExtended()
algorithm := string(metadata[s3_constants.AmzServerSideEncryption])
_, sseC := metadata[s3_constants.SeaweedFSSSEIV]
_, sseKMS := metadata[s3_constants.SeaweedFSSSEKMSKey]
_, sseS3 := metadata[s3_constants.SeaweedFSSSES3Key]
switch {
case sseC && metadata[s3_constants.AmzServerSideEncryptionCustomerAlgorithm] != nil:
le.sse = filer_pb.SSEType_SSE_C
case sseKMS && algorithm == s3_constants.SSEAlgorithmKMS:
le.sse = filer_pb.SSEType_SSE_KMS
case sseS3 && algorithm == s3_constants.SSEAlgorithmAES256:
le.sse = filer_pb.SSEType_SSE_S3
default:
le.sse = filer_pb.SSEType_NONE
}
for _, chunk := range le.Entry.GetChunks() {
if t := chunk.GetSseType(); t != filer_pb.SSEType_NONE && (le.sse == filer_pb.SSEType_NONE || le.sse == filer_pb.SSEType_SSE_S3) {
le.sse = t
}
}
})
return le.sse
}
// A file cut to zero bytes no longer holds the object's encrypted payload.
func clearS3Encryption(entry *filer_pb.Entry) {
for _, key := range []string{
s3_constants.SeaweedFSSSEIV,
s3_constants.AmzServerSideEncryptionCustomerAlgorithm,
s3_constants.AmzServerSideEncryptionCustomerKeyMD5,
s3_constants.SeaweedFSSSEKMSKey,
s3_constants.AmzServerSideEncryptionAwsKmsKeyId,
s3_constants.SeaweedFSSSES3Key,
s3_constants.AmzServerSideEncryption,
} {
delete(entry.Extended, key)
}
}
func (le *LockedEntry) GetEntry() *filer_pb.Entry {
le.RLock()
defer le.RUnlock()
return le.Entry
}
// SetEntry sets the entry of the LockedEntry
// entry should never be nil
func (le *LockedEntry) SetEntry(entry *filer_pb.Entry) {
le.Lock()
defer le.Unlock()
le.Entry = entry
le.sseCheck = sync.Once{}
}
func (le *LockedEntry) UpdateEntry(fn func(entry *filer_pb.Entry)) *filer_pb.Entry {
le.Lock()
defer le.Unlock()
fn(le.Entry)
le.sseCheck = sync.Once{}
return le.Entry
}
func (le *LockedEntry) GetChunks() []*filer_pb.FileChunk {
le.RLock()
defer le.RUnlock()
return le.Entry.Chunks
}
func (le *LockedEntry) AppendChunks(newChunks []*filer_pb.FileChunk) {
le.Lock()
defer le.Unlock()
le.Entry.Chunks = append(le.Entry.Chunks, newChunks...)
le.sseCheck = sync.Once{}
}