From c9e78d15e1e05f23facf7c28758b442bb25bde68 Mon Sep 17 00:00:00 2001 From: Christian Schwede Date: Wed, 29 May 2019 11:37:54 +0200 Subject: [PATCH] Remove invalid dict entries from hashes.pkl If the data in a hashes.pkl is corrupted but still de-serialized without errors, it will mess up the replication and gets never fixed. This happens for example if one of the keys is a NULL byte. This patch checks if the dict keys in hashes.pkl are valid strings and invalidates it if not. Closes-Bug: 1830881 Change-Id: I84b062d062ff49935feed0aee3e1963bb72eb5ea --- swift/obj/diskfile.py | 6 ++++++ test/unit/obj/test_diskfile.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/swift/obj/diskfile.py b/swift/obj/diskfile.py index fc542a9f0a..7ff5162647 100644 --- a/swift/obj/diskfile.py +++ b/swift/obj/diskfile.py @@ -300,6 +300,12 @@ def read_hashes(partition_dir): # given invalid input depending on the way in which the # input is invalid. pass + + # Check for corrupted data that could break os.listdir() + for suffix in hashes.keys(): + if not suffix.isalnum(): + return {'valid': False} + # hashes.pkl w/o valid updated key is "valid" but "forever old" hashes.setdefault('valid', True) hashes.setdefault('updated', -1) diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py index 8b05efc603..95cf90154e 100644 --- a/test/unit/obj/test_diskfile.py +++ b/test/unit/obj/test_diskfile.py @@ -8104,6 +8104,11 @@ class TestHashesHelpers(unittest.TestCase): # with the exactly the same value mutation from write_hashes self.assertEqual(hashes, result) + def test_ignore_corrupted_hashes(self): + corrupted_hashes = {u'\x00\x00\x00': False, 'valid': True} + diskfile.write_hashes(self.testdir, corrupted_hashes) + result = diskfile.read_hashes(self.testdir) + self.assertFalse(result['valid']) if __name__ == '__main__': unittest.main()