diff --git a/tundra/tests/test_persistent_state.py b/tundra/tests/test_persistent_state.py index 64183291..b0345e67 100644 --- a/tundra/tests/test_persistent_state.py +++ b/tundra/tests/test_persistent_state.py @@ -24,7 +24,8 @@ class PersistentStateTests(unittest.TestCase): def validate(self): self.path.write_text(json.dumps(self.value)) - with patch.object(state,'digest',return_value='a'*64): + with patch.object(state,'digest',return_value='a'*64), \ + patch.object(state,'embedded_system_sha',return_value='b'*64): return state.validate_manifest(self.path) def test_valid_prepared_state(self): @@ -48,5 +49,12 @@ class PersistentStateTests(unittest.TestCase): self.value['filesystemCheckPassed']=False with self.assertRaisesRegex(ValueError,'verification'):self.validate() + def test_system_image_mismatch_in_data_image_rejected(self): + self.path.write_text(json.dumps(self.value)) + with patch.object(state,'digest',return_value='a'*64), \ + patch.object(state,'embedded_system_sha',return_value='c'*64): + with self.assertRaisesRegex(ValueError,'different system image'): + state.validate_manifest(self.path) + if __name__=='__main__':unittest.main() diff --git a/tundra/tools/persistent_state.py b/tundra/tools/persistent_state.py index b7f7f4ac..2f5b065e 100644 --- a/tundra/tools/persistent_state.py +++ b/tundra/tools/persistent_state.py @@ -3,12 +3,27 @@ import hashlib import json import re +import subprocess import uuid from pathlib import Path FEATURES = 'none,has_journal,ext_attr,dir_index,filetype,extent,sparse_super,large_file,uninit_bg,dir_nlink,extra_isize' +def embedded_system_sha(image): + """Read the immutable-system binding stored inside a Tundra data image.""" + try: + result = subprocess.run( + ['debugfs', '-R', 'cat /system.sha256', str(image)], + check=True, capture_output=True, text=True, timeout=30) + except (OSError, subprocess.SubprocessError) as error: + raise ValueError('Cannot inspect the data image system binding') from error + value = result.stdout.strip() + if not re.fullmatch(r'[a-f0-9]{64}', value): + raise ValueError('Data image has no valid embedded system binding') + return value + + def digest(path): with Path(path).open('rb') as stream: return hashlib.file_digest(stream, 'sha256').hexdigest() @@ -33,6 +48,8 @@ def validate_manifest(path): image = path.parent/value['image'] if image.is_symlink() or image.stat().st_size != size or digest(image) != value['imageSha256']: raise ValueError('Initial data image changed') + if embedded_system_sha(image) != value['systemImageSha256']: + raise ValueError('Data image is bound to a different system image') if value.get('filesystemCheckPassed') is not True: raise ValueError('Data filesystem has not passed verification') return value