From 6ea7b10d94cec49c31a7e483a5995c76cfabaf86 Mon Sep 17 00:00:00 2001 From: maxjivi05 <6025299+maxjivi05@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:13:56 -0500 Subject: [PATCH] fix: drain archive padding before waiting on decoders --- tools/playstore/split_archive.py | 9 +++++ tools/tests/test_playstore_split_archive.py | 38 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/tools/playstore/split_archive.py b/tools/playstore/split_archive.py index 1aa55db5e..12e36d46e 100644 --- a/tools/playstore/split_archive.py +++ b/tools/playstore/split_archive.py @@ -34,6 +34,12 @@ def data_file(member, magic): return False +def finish_archive(archive): + for block in iter(lambda: archive.fileobj.read(65536), b''): + if block.strip(b'\0'): + raise ValueError('Nonzero data follows the tar end marker') + + def inventory(source, zstd): process = subprocess.Popen([zstd, '-dc', str(source)], stdout=subprocess.PIPE) records = [] @@ -65,6 +71,7 @@ def inventory(source, zstd): {key: value for key, value in member.pax_headers.items() if key not in {'path', 'linkpath', 'mtime', 'size', 'uid', 'gid', 'uname', 'gname'}}, digest.hexdigest() if member.isfile() else None]) + finish_archive(archive) if process.wait(): raise ValueError('Archive decompression failed') finally: @@ -106,6 +113,7 @@ def prepare(source, code, data, zstd_version='1.5.5'): encoder.stdin.write(block) remaining -= len(block) encoder.stdin.write(b'\0' * (-member.size % 512)) + finish_archive(archive) if kind == 'data': encoder.stdin.write(b'\0' * 10240) encoder.stdin.close() @@ -140,6 +148,7 @@ def validate_data(source): if not member.isfile() or not data_file(member, archive.extractfile(member).read(16)): raise ValueError('Executable or unclassified content in a Play data segment') found = True + finish_archive(archive) if process.wait() or not found: raise ValueError('Invalid Play data segment') finally: diff --git a/tools/tests/test_playstore_split_archive.py b/tools/tests/test_playstore_split_archive.py index 76cfe5b46..9383d07db 100644 --- a/tools/tests/test_playstore_split_archive.py +++ b/tools/tests/test_playstore_split_archive.py @@ -2,6 +2,7 @@ import io import re from pathlib import Path import subprocess +import sys import tarfile import tempfile import unittest @@ -61,3 +62,40 @@ class PlaySplitArchiveTest(unittest.TestCase): source = self.archive([('../font.ttf', b'\x00\x01\x00\x00font', 0o644)]) with self.assertRaisesRegex(ValueError, 'unsafe'): inventory(source, 'zstd') + + def test_padded_archives_finish_with_small_decoder_pipes(self): + source = self.archive([('program', b'\x7fELFcode', 0o755), + ('font.ttf', b'\x00\x01\x00\x00font', 0o644)]) + raw = subprocess.check_output(['zstd', '-dc', str(source)]) + source.write_bytes(subprocess.check_output(['zstd', '-c'], input=raw + b'\0' * (1024 * 1024))) + script = ''' +import subprocess +import sys +from pathlib import Path +from unittest.mock import patch +from tools.playstore.split_archive import inventory, prepare, combine, validate_data +original = subprocess.Popen +def spawn(*args, **kwargs): + if kwargs.get('stdout') == subprocess.PIPE or kwargs.get('stdin') == subprocess.PIPE: + kwargs['pipesize'] = 4096 + return original(*args, **kwargs) +root = Path(sys.argv[1]) +with patch.object(subprocess, 'Popen', spawn): + expected = inventory(root / 'source', 'zstd')[0] + prepare(root / 'source', root / 'code', root / 'data', sys.argv[2]) + combine([root / 'code', root / 'data'], root / 'combined') + assert inventory(root / 'combined', 'zstd')[0] == expected + validate_data(root / 'data') +print('padded archive completed') +''' + result = subprocess.run([sys.executable, '-c', script, str(self.root), self.zstd_version], + capture_output=True, text=True, timeout=15) + self.assertEqual(0, result.returncode, result.stdout + result.stderr) + self.assertIn('padded archive completed', result.stdout) + + def test_data_after_the_end_marker_is_rejected(self): + source = self.archive([('font.ttf', b'\x00\x01\x00\x00font', 0o644)]) + raw = subprocess.check_output(['zstd', '-dc', str(source)]) + source.write_bytes(subprocess.check_output(['zstd', '-c'], input=raw + b'\x7fELFhidden')) + with self.assertRaisesRegex(ValueError, 'Nonzero data'): + validate_data(source)