mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
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>
This commit is contained in:
1 parent
35caf2542c
commit
d0661afad8
9 files changed
+109
-28
No files matched your search
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
)
|
||||
|
||||
var errNativeSSES3Read = errors.New("native mount cannot decrypt SSE-S3 data")
|
||||
var errNativeSSERead = errors.New("native mount cannot decrypt S3 server-side encrypted data")
|
||||
|
||||
func (fh *FileHandle) lockForRead(startOffset int64, size int) {
|
||||
fh.dirtyPages.LockForRead(startOffset, startOffset+int64(size))
|
||||
@@ -66,11 +66,11 @@ func (fh *FileHandle) readFromChunksWithContext(ctx context.Context, buff []byte
|
||||
if fileSize == 0 {
|
||||
fileSize = int64(filer.FileSize(pbEntry))
|
||||
}
|
||||
if entry.hasSSES3DataLocked() {
|
||||
if sse := entry.sseTypeLocked(); sse != filer_pb.SSEType_NONE {
|
||||
reader := fh.wfs.option.SSES3Reader
|
||||
if reader == nil {
|
||||
if sse != filer_pb.SSEType_SSE_S3 || reader == nil {
|
||||
entry.RUnlock()
|
||||
return 0, 0, errNativeSSES3Read
|
||||
return 0, 0, errNativeSSERead
|
||||
}
|
||||
etag := sseObjectETag(pbEntry)
|
||||
version := string(pbEntry.GetExtended()[s3_constants.ExtVersionIdKey])
|
||||
|
||||
+44
-17
@@ -1,7 +1,6 @@
|
||||
package mount
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
@@ -11,30 +10,58 @@ import (
|
||||
type LockedEntry struct {
|
||||
*filer_pb.Entry
|
||||
sync.RWMutex
|
||||
sseS3Check sync.Once
|
||||
sseS3 bool
|
||||
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.
|
||||
func (le *LockedEntry) hasSSES3Data() bool {
|
||||
// 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.hasSSES3DataLocked()
|
||||
return le.sseTypeLocked()
|
||||
}
|
||||
|
||||
func (le *LockedEntry) hasSSES3DataLocked() bool {
|
||||
le.sseS3Check.Do(func() {
|
||||
func (le *LockedEntry) sseTypeLocked() filer_pb.SSEType {
|
||||
le.sseCheck.Do(func() {
|
||||
metadata := le.Entry.GetExtended()
|
||||
_, hasKey := metadata[s3_constants.SeaweedFSSSES3Key]
|
||||
le.sseS3 = hasKey && string(metadata[s3_constants.AmzServerSideEncryption]) == s3_constants.SSEAlgorithmAES256
|
||||
if !le.sseS3 {
|
||||
le.sseS3 = slices.ContainsFunc(le.Entry.GetChunks(), func(chunk *filer_pb.FileChunk) bool {
|
||||
return chunk.GetSseType() == filer_pb.SSEType_SSE_S3
|
||||
})
|
||||
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.sseS3
|
||||
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 {
|
||||
@@ -49,14 +76,14 @@ func (le *LockedEntry) SetEntry(entry *filer_pb.Entry) {
|
||||
le.Lock()
|
||||
defer le.Unlock()
|
||||
le.Entry = entry
|
||||
le.sseS3Check = sync.Once{}
|
||||
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.sseS3Check = sync.Once{}
|
||||
le.sseCheck = sync.Once{}
|
||||
return le.Entry
|
||||
}
|
||||
|
||||
@@ -70,5 +97,5 @@ func (le *LockedEntry) AppendChunks(newChunks []*filer_pb.FileChunk) {
|
||||
le.Lock()
|
||||
defer le.Unlock()
|
||||
le.Entry.Chunks = append(le.Entry.Chunks, newChunks...)
|
||||
le.sseS3Check = sync.Once{}
|
||||
le.sseCheck = sync.Once{}
|
||||
}
|
||||
@@ -176,7 +176,7 @@ func sseObjectETag(entry *filer_pb.Entry) string {
|
||||
func (r *SSES3Reader) readAt(ctx context.Context, fullPath, etag, version string, size int64, buf []byte, offset int64) (int64, error) {
|
||||
key, ok := strings.CutPrefix(fullPath, r.root)
|
||||
if !ok || key == "" || key == "." || key == ".." || path.IsAbs(key) || path.Clean(key) != key || strings.HasPrefix(key, "../") {
|
||||
return 0, errNativeSSES3Read
|
||||
return 0, errNativeSSERead
|
||||
}
|
||||
versionFile := false
|
||||
if dir, name := path.Split(key); version != "" && name == "v_"+version {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -178,6 +179,12 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse
|
||||
entry.Attributes.Mtime = truncNow.Unix()
|
||||
entry.Attributes.MtimeNs = int32(truncNow.Nanosecond())
|
||||
entry.Attributes.FileSize = size
|
||||
if size == 0 {
|
||||
clearS3Encryption(entry)
|
||||
if fh != nil {
|
||||
fh.entry.sseCheck = sync.Once{}
|
||||
}
|
||||
}
|
||||
if size > oldFileSize {
|
||||
// The writes that fill the range will not grow the file, so they
|
||||
// charge nothing; the growth is counted here or the quota never
|
||||
|
||||
@@ -109,7 +109,7 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn)
|
||||
if fhIn.entry.IsDirectory || fhOut.entry.IsDirectory {
|
||||
return 0, fuse.EISDIR
|
||||
}
|
||||
if fhIn.entry.hasSSES3Data() {
|
||||
if fhIn.entry.sseType() != filer_pb.SSEType_NONE {
|
||||
return 0, fuse.ENOTSUP
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn)
|
||||
numBytesRead, err := readDataByFileHandle(
|
||||
buff[:readLen], fhIn, offsetIn)
|
||||
if err != nil {
|
||||
if err == errNativeSSES3Read {
|
||||
if err == errNativeSSERead {
|
||||
return 0, fuse.ENOTSUP
|
||||
}
|
||||
glog.Warningf("file handle read %s %d (total %d): %v",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/seaweedfs/go-fuse/v2/fuse"
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -70,8 +71,9 @@ func (wfs *WFS) Open(cancel <-chan struct{}, in *fuse.OpenIn, out *fuse.OpenOut)
|
||||
defer cancelFunc()
|
||||
fileHandle, openFh, status = wfs.AcquireHandle(ctx, in.NodeId, in.Flags, in.Uid, in.Gid)
|
||||
if status == fuse.OK {
|
||||
encrypted := openFlagsToAccessMask(in.Flags)&fuse.R_OK != 0 && fileHandle.GetEntry().hasSSES3Data()
|
||||
if encrypted && wfs.option.SSES3Reader == nil {
|
||||
sse := fileHandle.GetEntry().sseType()
|
||||
encrypted := openFlagsToAccessMask(in.Flags)&fuse.R_OK != 0 && sse != filer_pb.SSEType_NONE
|
||||
if encrypted && (sse != filer_pb.SSEType_SSE_S3 || wfs.option.SSES3Reader == nil) {
|
||||
wfs.ReleaseHandle(openFh)
|
||||
return fuse.ENOTSUP
|
||||
}
|
||||
|
||||
@@ -625,6 +625,7 @@ func (wfs *WFS) truncateEntry(entryFullPath util.FullPath, entry *filer_pb.Entry
|
||||
entry.Content = nil
|
||||
entry.Chunks = nil
|
||||
entry.Attributes.FileSize = 0
|
||||
clearS3Encryption(entry)
|
||||
truncNow := time.Now()
|
||||
entry.Attributes.Mtime = truncNow.Unix()
|
||||
entry.Attributes.MtimeNs = int32(truncNow.Nanosecond())
|
||||
|
||||
@@ -52,7 +52,7 @@ func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse
|
||||
|
||||
offset := int64(in.Offset)
|
||||
totalRead, err := readDataByFileHandleWithContext(ctx, buff, fh, offset)
|
||||
if err != nil && err != errNativeSSES3Read && err != errSSES3Permission && ctx.Err() == nil {
|
||||
if err != nil && err != errNativeSSERead && err != errSSES3Permission && ctx.Err() == nil {
|
||||
// The chunk views a read resolves can name data a concurrent overwrite
|
||||
// already replaced: the filer queues replaced file ids for deletion at
|
||||
// commit while this handle only learns the new list when the metadata
|
||||
@@ -66,7 +66,7 @@ func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if err == errNativeSSES3Read {
|
||||
if err == errNativeSSERead {
|
||||
return nil, fuse.ENOTSUP
|
||||
}
|
||||
if err == errSSES3Permission {
|
||||
|
||||
@@ -15,10 +15,14 @@ func TestNativeReadSSES3(t *testing.T) {
|
||||
content := []byte("test payload")
|
||||
header := s3_constants.AmzServerSideEncryption
|
||||
key := s3_constants.SeaweedFSSSES3Key
|
||||
kmsKey := s3_constants.SeaweedFSSSEKMSKey
|
||||
customer := s3_constants.AmzServerSideEncryptionCustomerAlgorithm
|
||||
iv := s3_constants.SeaweedFSSSEIV
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
extended map[string][]byte
|
||||
chunks []*filer_pb.FileChunk
|
||||
reader bool
|
||||
want fuse.Status
|
||||
}{
|
||||
{name: "plain", want: fuse.OK},
|
||||
@@ -26,10 +30,19 @@ func TestNativeReadSSES3(t *testing.T) {
|
||||
{name: "native cipher key", chunks: []*filer_pb.FileChunk{{CipherKey: []byte("native test key")}}, want: fuse.OK},
|
||||
{name: "object metadata", extended: map[string][]byte{header: []byte("AES256"), key: []byte("test metadata")}, want: fuse.ENOTSUP},
|
||||
{name: "multipart chunk metadata", chunks: []*filer_pb.FileChunk{{}, {SseType: filer_pb.SSEType_SSE_S3, SseMetadata: []byte("test metadata")}}, want: fuse.ENOTSUP},
|
||||
{name: "stale kms header", extended: map[string][]byte{header: []byte("aws:kms")}, want: fuse.OK},
|
||||
{name: "kms object metadata", extended: map[string][]byte{header: []byte("aws:kms"), kmsKey: []byte("test metadata")}, reader: true, want: fuse.ENOTSUP},
|
||||
{name: "kms chunk metadata", chunks: []*filer_pb.FileChunk{{SseType: filer_pb.SSEType_SSE_KMS, SseMetadata: []byte("test metadata")}}, reader: true, want: fuse.ENOTSUP},
|
||||
{name: "sse-c object metadata", extended: map[string][]byte{customer: []byte("AES256"), iv: []byte("test iv")}, reader: true, want: fuse.ENOTSUP},
|
||||
{name: "sse-c chunk metadata", chunks: []*filer_pb.FileChunk{{SseType: filer_pb.SSEType_SSE_C, SseMetadata: []byte("test metadata")}}, reader: true, want: fuse.ENOTSUP},
|
||||
{name: "sse-s3 object with a kms part", extended: map[string][]byte{header: []byte("AES256"), key: []byte("test metadata")}, chunks: []*filer_pb.FileChunk{{SseType: filer_pb.SSEType_SSE_S3}, {SseType: filer_pb.SSEType_SSE_KMS}}, reader: true, want: fuse.ENOTSUP},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
wfs := newInvalidateTestWFS(t)
|
||||
wfs.openMtimeCache = make(map[uint64][2]int64)
|
||||
if tc.reader {
|
||||
wfs.option.SSES3Reader = gatewayReader(t, "")
|
||||
}
|
||||
inode := wfs.inodeToPath.Lookup(util.FullPath("/file"), 1, false, false, 0, false)
|
||||
entry := &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{FileSize: uint64(len(content)), FileMode: 0o644}, Content: content}
|
||||
fh, id, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, entry, 0, 0)
|
||||
@@ -72,3 +85,34 @@ func TestNativeReadSSES3(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateClearsS3Encryption(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
size uint64
|
||||
want filer_pb.SSEType
|
||||
}{
|
||||
{size: 4, want: filer_pb.SSEType_SSE_KMS},
|
||||
{size: 0, want: filer_pb.SSEType_NONE},
|
||||
} {
|
||||
wfs, inode, fh := newUnlinkedOpenFile(t)
|
||||
fh.entry.SetEntry(&filer_pb.Entry{
|
||||
Name: "file",
|
||||
Attributes: &filer_pb.FuseAttributes{FileMode: 0o644, FileSize: 8},
|
||||
Extended: map[string][]byte{s3_constants.AmzServerSideEncryption: []byte("aws:kms"), s3_constants.SeaweedFSSSEKMSKey: []byte("test metadata")},
|
||||
Chunks: []*filer_pb.FileChunk{{FileId: "1,01", Size: 8, SseType: filer_pb.SSEType_SSE_KMS}},
|
||||
})
|
||||
if got := fh.GetEntry().sseType(); got != filer_pb.SSEType_SSE_KMS {
|
||||
t.Fatalf("before truncate: sse = %v", got)
|
||||
}
|
||||
in := &fuse.SetAttrIn{}
|
||||
in.NodeId = inode
|
||||
in.Valid = fuse.FATTR_SIZE
|
||||
in.Size = tc.size
|
||||
if status := wfs.SetAttr(nil, in, &fuse.AttrOut{}); status != fuse.OK {
|
||||
t.Fatalf("truncate to %d: %v", tc.size, status)
|
||||
}
|
||||
if got := fh.GetEntry().sseType(); got != tc.want {
|
||||
t.Errorf("truncate to %d: sse = %v, want %v", tc.size, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user