Repository navigation
Conversation
A self-copy uses it to read the source data and find the master key that decrypts it; the code is the same as on 9.4.
Hello dvasilas,My role is to assist you with the merge of this Available options
Available commands
Status report is not available. |
Incorrect fix versionThe
Considering where you are trying to merge, I ignored possible hotfix versions and I expected to find:
Please check the |
3c6de9a to
479bc28
Compare
Incorrect fix versionThe
Considering where you are trying to merge, I ignored possible hotfix versions and I expected to find:
Please check the |
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Request integration branchesWaiting for integration branch creation to be requested by the user. To request integration branches, please comment on this pull request with the following command: Alternatively, the |
…rites it Until CLDSRV-910, a self-copy to another master key changed only the metadata and left the data encrypted under the old one; a self-copy that rewrites such data decrypts it with the wrong key and replaces the content with wrong bytes.
479bc28 to
98543fe
Compare
| const rank = part => (part.size === 0 ? Infinity : part.size); | ||
| return parts.reduce((smallest, part) => { | ||
| const match = partETagPattern.exec(part.dataStoreETag); | ||
| if (!match || !part.locations.every(location => location.cipheredDataKey)) { | ||
| return smallest; | ||
| } | ||
| if (smallest && rank(smallest) <= rank(part)) { | ||
| return smallest; | ||
| } | ||
| return { locations: part.locations, md5: match[1], size: part.size }; | ||
| }, null); |
There was a problem hiding this comment.
Minor (rare, but the loss is permanent): a wrong key can pass the check on a tiny last part
- Cause: the check decrypts only the smallest part that has an MD5 (
sourceMasterKey.js:41-51). With a KMS that never rejects a wrong master key (file and memory), a wrong key turns a one-byte part into the right byte once in 256. - Effect: the check accepts that key, the copy decrypts every other part with it, stores the result and deletes the original. The client gets 200.
- Odds: 1 in 256 for a one-byte part, 1 in 65,536 for two bytes, 1 in 16.7 million for three, per wrong key tried before the right one. The metadata key is tried first, so every damaged object is exposed to it. Retrying does not change the outcome for a given key and object.
- Not affected: objects that are not multipart (the whole object is checked, so a match means the content is right), and a KMS that rejects a wrong key.
- Test:
should not accept a key that decrypts only a one-byte last part(code below). It builds a 5 MiB + 1 byte multipart object under K1, creates keys until one decrypts the last byte correctly, sets it as the metadata key, and self-copies to K3:AssertionError [ERR_ASSERTION]: the copy stored wrong content - Fix: do not trust a part of a few bytes alone. Add the next smallest parts until at least 16 bytes are checked.
Test
Goes in describe('which master key decrypts the data') in tests/unit/api/objectCopy.js.
it('should not accept a key that decrypts only a one-byte last part', async function test() {
this.timeout(60000);
const { promisify } = require('util');
const parts = [Buffer.alloc(constants.minimumAllowedPartSize, 'a'), Buffer.from('z')];
const [k1, k3] = await promisify(createKeys)(2);
// A two-part object under k1, whose last part is one byte.
const uploadId = await promisify(initiateMpu)(objectKey, kmsHeaders(k1));
for (let i = 0; i < parts.length; i++) {
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve, reject) =>
objectPutPart(authInfo, partRequest(objectKey, uploadId, i + 1, parts[i]), undefined, log, err =>
(err ? reject(err) : resolve()),
),
);
}
await promisify(completeMpu)(objectKey, uploadId, parts.map(md5));
const { location } = await promisify(getObjectMD)();
const last = location[location.length - 1];
// Decrypts the stored byte of the last part with a master key, as data.get does.
const lastByteWith = masterKeyId =>
new Promise((resolve, reject) =>
kms.createDecipherBundle(
{
cryptoScheme: last.cryptoScheme,
masterKeyId,
cipheredDataKey: Buffer.from(last.cipheredDataKey, 'base64'),
},
0,
log,
(err, bundle) => (err ? reject(err) : resolve(bundle.decipher.update(ds[last.key].value))),
),
);
// Create keys until one decrypts that byte to 'z': 1 key in 256.
let k2;
for (let i = 0; i < 20000 && !k2; i++) {
// eslint-disable-next-line no-await-in-loop
const candidate = await promisify(createKey)();
// eslint-disable-next-line no-await-in-loop
if ((await lastByteWith(candidate)).equals(parts[1])) {
k2 = candidate;
}
}
assert(k2, 'no such key found');
// The damaged state: data under k1, which the locations record, and k2 in the metadata.
await promisify(updateObjectMD)(md => {
// eslint-disable-next-line no-param-reassign
md['x-amz-server-side-encryption-aws-kms-key-id'] = k2;
recordKeyOnLocations(md, k1);
});
await promisify(selfCopy)(kmsHeaders(k3));
const body = await promisify(getBody)();
assert(body.equals(Buffer.concat(parts)), 'the copy stored wrong content');
});
TLDR
#6346 fixed CLDSRV-910.
But after the fix, a self-copy of an object damaged by CLDSRV-910 to a master key other than the one in its metadata replaces its content with the wrong content and deletes the original.
With this PR, before such a self-copy rewrites the data, it checks which master key decrypts it and uses that key. If none of the keys it tries does, the copy fails and nothing is written.
The original bug (CLDSRV-910)
Using a self-copy to re-encrypt an object under another master key changed only the master key ID in the object metadata. The data and its data key stayed encrypted under the old master key.
Reading such an object unwraps the data key with the wrong master key: either GET returns incorrect content, or it fails if KMS detects the wrong key.
Before the CLDSRV-910 fix, a self-copy naming the old master key repaired the object: it changed the master key ID in the metadata back to the old key.
Problem
Since the CLDSRV-910 fix, a self-copy to a master key other than the one in the object metadata rewrites the data.
Here is what happens if such a self-copy is done to an object damaged by CLDSRV-910, even when it names the master key the data is encrypted under.
The self-copy:
The original content can no longer be recovered.
Since the CLDSRV-910 fix, a self-copy rewrites the data.
Here is what happens if a self-copy with another master key is done to an object damaged by CLDSRV-910.
The self-copy:
The object can no longer be repaired.
This affects only objects damaged by CLDSRV-910.
Self-copy for objects not affected by CLDSRV-910 works as expected.
Fix
With this PR, before rewriting the data, a self-copy decrypts the smallest encrypted part with up to 3 master keys, in this order: the master key in the object metadata, the master key recorded on the locations, and the destination master key.
It compares the MD5 of the decrypted part with the MD5 recorded for that part.
The copy reads the data with the first key that matches. If no key matches, or the part has no recorded MD5, the copy fails with
InternalError, and nothing is written.For example, the data is encrypted under K1 and the metadata names K2.
K2 doesn't match; K1 does:
When the new check runs
When the check runs, it costs one read of the smallest part and one KMS decrypt per key tried.
This check runs only when
This covers any object updated by a self-copy that didn't rewrite the data (this includes objects damaged by CLDSRV-910), even if its metadata no longer says it is encrypted. A self-copy without SSE, in a bucket without default encryption, removes the encryption from the metadata but leaves the data encrypted.
Other objects are rewritten as before, without the extra read.
Notes
InternalError.x-amz-metadata-directive: REPLACEwithx-amz-meta-scal-location-constraint). That copy always rewrites the data, so it replaced damaged objects with the wrong content before CLDSRV-910 too.