Skip to content

Commit fd76d16

Browse files
Fix stale metadata with disabled encryption (#16887) (#16888) (#16998)
Co-authored-by: Cthulhu <cthulhu@ydb.tech>
1 parent b6d3012 commit fd76d16

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

ydb/core/blobstorage/pdisk/blobstorage_pdisk_data.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,28 @@ struct TMetadataHeader {
428428
*this = header;
429429
}
430430

431-
bool CheckHash() const {
431+
bool CheckHash(ui64 *magic) const {
432432
TPDiskHashCalculator hasher;
433+
#ifdef DISABLE_PDISK_ENCRYPTION
434+
if (magic) {
435+
hasher.Hash(magic, sizeof(ui64));
436+
}
437+
#else
438+
Y_UNUSED(magic);
439+
#endif
433440
hasher.Hash(this, sizeof(TMetadataHeader) - sizeof(THash));
434441
return hasher.GetHashResult() == HeaderHash;
435442
}
436443

437-
void SetHash() {
444+
void SetHash(const ui64 *magic) {
438445
TPDiskHashCalculator hasher;
446+
#ifdef DISABLE_PDISK_ENCRYPTION
447+
if (magic) {
448+
hasher.Hash(magic, sizeof(ui64));
449+
}
450+
#else
451+
Y_UNUSED(magic);
452+
#endif
439453
hasher.Hash(this, sizeof(TMetadataHeader) - sizeof(THash));
440454
HeaderHash = hasher.GetHashResult();
441455
}

ydb/core/blobstorage/pdisk/blobstorage_pdisk_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class TPDisk : public IPDisk {
425425
void DropAllMetadataRequests();
426426

427427
TRcBuf CreateMetadataPayload(TRcBuf& metadata, size_t offset, size_t payloadSize, ui32 sectorSize, bool encryption,
428-
const TKey& key, ui64 sequenceNumber, ui32 recordIndex, ui32 totalRecords);
428+
const TKey& key, ui64 sequenceNumber, ui32 recordIndex, ui32 totalRecords, const ui64 *magic);
429429
bool WriteMetadataSync(TRcBuf&& metadata, const TDiskFormat& format);
430430

431431
static std::optional<TMetadataFormatSector> CheckMetadataFormatSector(const ui8 *data, size_t len, const TMainKey& mainKey);

ydb/core/blobstorage/pdisk/blobstorage_pdisk_impl_metadata.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace NKikimr::NPDisk {
4141
// obtain the header and decrypt its encrypted part, then validate the hash
4242
TMetadataHeader *header = reinterpret_cast<TMetadataHeader*>(GetBuffer());
4343
header->Encrypt(cypher);
44-
if (header->CheckHash()) {
44+
if (header->CheckHash((ui64*)(void*)&PDisk->Format.ChunkKey)) {
4545
// check we have read it all
4646
const ui32 total = sizeof(TMetadataHeader) + header->Length;
4747
if (total <= Buffer.size()) {
@@ -167,7 +167,7 @@ namespace NKikimr::NPDisk {
167167

168168
auto *header = reinterpret_cast<TMetadataHeader*>(Buffer.GetDataMut());
169169
header->Encrypt(cypher);
170-
if (!header->CheckHash()) {
170+
if (!header->CheckHash((ui64*)(void*)&Format.DataKey)) {
171171
req->ErrorReason = "header has is not valid";
172172
} else if (header->TotalRecords != 1 || header->RecordIndex != 0 || header->SequenceNumber != 0) {
173173
req->ErrorReason = "header fields are filled incorrectly";
@@ -593,7 +593,7 @@ namespace NKikimr::NPDisk {
593593

594594
const size_t payloadSize = Min<size_t>(slotSize, metadataSize - offset);
595595
TRcBuf payload = CreateMetadataPayload(write.Metadata, offset, payloadSize, Format.SectorSize,
596-
Cfg->EnableSectorEncryption, Format.ChunkKey, Meta.NextSequenceNumber, i, numSlotsRequired);
596+
Cfg->EnableSectorEncryption, Format.ChunkKey, Meta.NextSequenceNumber, i, numSlotsRequired, (ui64*)(void*)&Format.ChunkKey);
597597

598598
completion->AddQuery(key, std::move(payload));
599599
completion->CostNs += DriveModel.TimeForSizeNs(payload.size(), key.ChunkIdx, TDriveModel::OP_TYPE_WRITE);
@@ -617,7 +617,7 @@ namespace NKikimr::NPDisk {
617617
}
618618

619619
TRcBuf payload = CreateMetadataPayload(write.Metadata, 0, write.Metadata.size(), DefaultSectorSize,
620-
true, fmt.DataKey, 0, 0, 1);
620+
true, fmt.DataKey, 0, 0, 1, (ui64*)(void*)&fmt.DataKey);
621621
const size_t bytesToWrite = payload.size();
622622

623623
ui64 rawDeviceSize = 0;
@@ -718,7 +718,7 @@ namespace NKikimr::NPDisk {
718718
}
719719

720720
TRcBuf TPDisk::CreateMetadataPayload(TRcBuf& metadata, size_t offset, size_t payloadSize, ui32 sectorSize,
721-
bool encryption, const TKey& key, ui64 sequenceNumber, ui32 recordIndex, ui32 totalRecords) {
721+
bool encryption, const TKey& key, ui64 sequenceNumber, ui32 recordIndex, ui32 totalRecords, const ui64 *magic) {
722722
Y_ABORT_UNLESS(offset + payloadSize <= metadata.size());
723723

724724
Y_DEBUG_ABORT_UNLESS(IsPowerOf2(sectorSize));
@@ -749,7 +749,7 @@ namespace NKikimr::NPDisk {
749749
.DataHash = dataHasher.GetHashResult(),
750750
};
751751

752-
header->SetHash();
752+
header->SetHash(magic);
753753
header->EncryptData(cypher);
754754
header->Encrypt(cypher);
755755

@@ -791,7 +791,7 @@ namespace NKikimr::NPDisk {
791791
const NMeta::TSlotKey key = freeSlotKeys[i];
792792
const size_t payloadSize = Min<size_t>(slotSize, metadataSize - offset);
793793
TRcBuf payload = CreateMetadataPayload(metadata, offset, payloadSize, format.SectorSize,
794-
Cfg->EnableSectorEncryption, format.ChunkKey, 1, i, numSlotsRequired);
794+
Cfg->EnableSectorEncryption, format.ChunkKey, 1, i, numSlotsRequired, (ui64*)(void*)&format.ChunkKey);
795795
BlockDevice->PwriteSync(payload.data(), payload.size(), format.Offset(key.ChunkIdx, key.OffsetInSectors), {}, nullptr);
796796
}
797797

0 commit comments

Comments
 (0)