Fix Sargo persistent state image binding

This commit is contained in:
OverSu 2026-11-21 02:11:14 +01:00
commit 6a44daeaaa
2 changed files with 26 additions and 1 deletions

View file

@ -24,7 +24,8 @@ class PersistentStateTests(unittest.TestCase):
def validate(self): def validate(self):
self.path.write_text(json.dumps(self.value)) 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) return state.validate_manifest(self.path)
def test_valid_prepared_state(self): def test_valid_prepared_state(self):
@ -48,5 +49,12 @@ class PersistentStateTests(unittest.TestCase):
self.value['filesystemCheckPassed']=False self.value['filesystemCheckPassed']=False
with self.assertRaisesRegex(ValueError,'verification'):self.validate() 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() if __name__=='__main__':unittest.main()

View file

@ -3,12 +3,27 @@
import hashlib import hashlib
import json import json
import re import re
import subprocess
import uuid import uuid
from pathlib import Path from pathlib import Path
FEATURES = 'none,has_journal,ext_attr,dir_index,filetype,extent,sparse_super,large_file,uninit_bg,dir_nlink,extra_isize' 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): def digest(path):
with Path(path).open('rb') as stream: with Path(path).open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest() return hashlib.file_digest(stream, 'sha256').hexdigest()
@ -33,6 +48,8 @@ def validate_manifest(path):
image = path.parent/value['image'] image = path.parent/value['image']
if image.is_symlink() or image.stat().st_size != size or digest(image) != value['imageSha256']: if image.is_symlink() or image.stat().st_size != size or digest(image) != value['imageSha256']:
raise ValueError('Initial data image changed') 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: if value.get('filesystemCheckPassed') is not True:
raise ValueError('Data filesystem has not passed verification') raise ValueError('Data filesystem has not passed verification')
return value return value