mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
Restrict reset diagnostics and create private output atomically
This commit is contained in:
1 parent
259fa57d5c
commit
cd24e503ba
3 files changed
+139
-9
No files matched your search
@@ -8,6 +8,7 @@ It emits fixed categories and numeric hardware fields, never arbitrary log lines
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
@@ -15,16 +16,22 @@ from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
ENTRY = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s+(SYSTEM_LAST_KMSG)\b', re.M)
|
||||
CATEGORIES = ('GNOC', 'PVRSRV', 'watchdog', 'IOMMU', 'S2MPU', 'access control', 'Kernel panic', 'HWR', 'GPU fault')
|
||||
PRIMARY_USER_STATES = frozenset(('RUNNING_LOCKED', 'RUNNING_UNLOCKING', 'RUNNING_UNLOCKED'))
|
||||
# Exact canonical reasons plus the vendor reason observed in the bounded Pixel
|
||||
# tests. Unrecognized/custom suffixes are not diagnostic output categories.
|
||||
BOOT_REASONS = frozenset(('cold', 'hard', 'warm', 'reboot', 'shutdown',
|
||||
'recovery', 'bootloader', 'kernel_panic', 'watchdog',
|
||||
'reset,warm,gnoc_error'))
|
||||
|
||||
|
||||
def primary_state(text):
|
||||
match = re.search(r'Started users state: \[0=(RUNNING_[A-Z_]+)', text)
|
||||
return match.group(1) if match else 'unknown'
|
||||
match = re.search(r'Started users state: \[0=([A-Z_]+)(?=[,\]])', text)
|
||||
return match.group(1) if match and match.group(1) in PRIMARY_USER_STATES else 'unknown'
|
||||
|
||||
|
||||
def safe_reason(text):
|
||||
value = text.strip()
|
||||
return value if re.fullmatch(r'[A-Za-z0-9_, .():-]{1,160}', value) else 'unknown'
|
||||
return value if value in BOOT_REASONS else 'unknown'
|
||||
|
||||
|
||||
def kernel_projection(text, device_timezone, after=None):
|
||||
@@ -56,8 +63,10 @@ def kernel_projection(text, device_timezone, after=None):
|
||||
'access_control_addresses': r'Access Control Error \((0x[0-9a-fA-F]+)',
|
||||
}
|
||||
result['hardware_fields'] = {key: sorted(set(re.findall(pattern, body))) for key, pattern in patterns.items()}
|
||||
reasons = re.findall(r'Last boot reason:\s*([A-Za-z0-9_,.-]+)', body)
|
||||
result['last_boot_reasons'] = sorted(set(reasons))
|
||||
# Read the complete value, not just a recognized-looking prefix. Apply the
|
||||
# same fixed projection as getprop; no arbitrary kernel reason text escapes.
|
||||
reasons = re.findall(r'Last boot reason:[ \t]*([^\r\n]*)', body)
|
||||
result['last_boot_reasons'] = sorted({safe_reason(reason) for reason in reasons})
|
||||
return result
|
||||
|
||||
|
||||
@@ -100,6 +109,15 @@ def collect(adb, serial, include_kernel=False, after=None):
|
||||
return result
|
||||
|
||||
|
||||
def write_private_output(path, output):
|
||||
# Exclusive creation also rejects existing symlinks. Set the private mode
|
||||
# before any bytes are written, rather than chmod after exposing the data.
|
||||
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_NOFOLLOW', 0)
|
||||
descriptor = os.open(path, flags, 0o600)
|
||||
with os.fdopen(descriptor, 'w', encoding='utf-8') as stream:
|
||||
stream.write(output)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--adb', default='adb')
|
||||
@@ -114,9 +132,7 @@ def main():
|
||||
result = collect(args.adb, args.serial, args.kernel, after)
|
||||
output = json.dumps(result, indent=2) + '\n'
|
||||
if args.output:
|
||||
with args.output.open('x') as stream:
|
||||
stream.write(output)
|
||||
args.output.chmod(0o600)
|
||||
write_private_output(args.output, output)
|
||||
print(output, end='')
|
||||
|
||||
|
||||
|
||||
@@ -2,8 +2,13 @@
|
||||
from datetime import datetime, timezone
|
||||
import importlib.util
|
||||
import json
|
||||
import io
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -46,6 +51,109 @@ class ResetProjectionTest(unittest.TestCase):
|
||||
self.assertEqual(module.safe_reason('https://private.invalid/login?secret=abc'), 'unknown')
|
||||
self.assertEqual(module.safe_reason('/data/private'), 'unknown')
|
||||
|
||||
def test_reason_only_recognized_exact_values(self):
|
||||
for reason in ('cold', 'hard', 'warm', 'reboot', 'shutdown', 'recovery',
|
||||
'bootloader', 'kernel_panic', 'watchdog', 'reset,warm,gnoc_error'):
|
||||
with self.subTest(reason=reason):
|
||||
self.assertEqual(module.safe_reason(reason + '\n'), reason)
|
||||
for reason in ('PRIVATE TEST VALUE', 'private_test_value', '123456',
|
||||
'reboot,private_test_value', 'reset,warm,gnoc_error,private_test_value',
|
||||
'reset,warm,gnoc_error trailing_private_test_value', ''):
|
||||
with self.subTest(reason=reason):
|
||||
self.assertEqual(module.safe_reason(reason), 'unknown')
|
||||
|
||||
def test_kernel_reason_uses_same_fixed_projection(self):
|
||||
raw = ('2026-10-11 17:35:01 SYSTEM_LAST_KMSG (text)\n'
|
||||
'Last boot reason: private_test_value\n'
|
||||
'Last boot reason: reset,warm,gnoc_error trailing_private_test_value\n'
|
||||
'Last boot reason: reset,warm,gnoc_error\n'
|
||||
'Last boot reason:\nPRIVATE TEST VALUE\n')
|
||||
result = module.kernel_projection(raw, 'America/Toronto')
|
||||
self.assertEqual(result['last_boot_reasons'], ['reset,warm,gnoc_error', 'unknown'])
|
||||
self.assertNotIn('private_test_value', json.dumps(result))
|
||||
self.assertNotIn('PRIVATE TEST VALUE', json.dumps(result))
|
||||
|
||||
def test_primary_state_uses_fixed_categories(self):
|
||||
for state in ('RUNNING_LOCKED', 'RUNNING_UNLOCKING', 'RUNNING_UNLOCKED'):
|
||||
self.assertEqual(module.primary_state('Started users state: [0=' + state + ']'), state)
|
||||
for state in ('RUNNING_PRIVATE_TEST_VALUE', 'RUNNING_UNLOCKED_PRIVATE',
|
||||
'RUNNING_UNLOCKED=private_test_value'):
|
||||
self.assertEqual(module.primary_state('Started users state: [0=' + state + ']'), 'unknown')
|
||||
|
||||
def test_property_reason_never_exports_custom_text(self):
|
||||
def run(argv, **kwargs):
|
||||
if argv[-2:] == ['getprop', 'ro.boot.bootreason']:
|
||||
text = 'PRIVATE TEST VALUE'
|
||||
elif argv[-2:] == ['getprop', 'sys.boot.reason']:
|
||||
text = 'reset,warm,gnoc_error,private_test_value'
|
||||
elif argv[-1] == 'persist.sys.timezone':
|
||||
text = 'America/Toronto'
|
||||
else:
|
||||
text = ''
|
||||
return subprocess.CompletedProcess(argv, 0, text, '')
|
||||
with patch.object(module.subprocess, 'run', side_effect=run):
|
||||
result = module.collect('adb', 'test-device')
|
||||
self.assertEqual(result['bootloader_reason'], 'unknown')
|
||||
self.assertEqual(result['system_boot_reason'], 'unknown')
|
||||
self.assertNotIn('PRIVATE TEST VALUE', json.dumps(result))
|
||||
self.assertNotIn('private_test_value', json.dumps(result))
|
||||
|
||||
@unittest.skipUnless(os.name == 'posix', 'POSIX private file modes')
|
||||
def test_cli_output_is_private_before_writing(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
output = Path(directory) / 'diagnostics.json'
|
||||
saved_umask = os.umask(0)
|
||||
original_fdopen = os.fdopen
|
||||
original_path_open = Path.open
|
||||
creation_modes = []
|
||||
def inspect_stream(stream):
|
||||
try:
|
||||
creation_modes.append(stat.S_IMODE(os.fstat(stream.fileno()).st_mode))
|
||||
self.assertEqual(os.fstat(stream.fileno()).st_size, 0)
|
||||
self.assertEqual(creation_modes[-1], 0o600)
|
||||
except BaseException:
|
||||
stream.close()
|
||||
raise
|
||||
return stream
|
||||
def inspect_fd(fd, *args, **kwargs):
|
||||
return inspect_stream(original_fdopen(fd, *args, **kwargs))
|
||||
def inspect_path(path, *args, **kwargs):
|
||||
stream = original_path_open(path, *args, **kwargs)
|
||||
return inspect_stream(stream) if path == output else stream
|
||||
try:
|
||||
with patch.object(sys, 'argv', ['diagnostics', '--serial', 'test-device',
|
||||
'--output', str(output)]), \
|
||||
patch.object(module, 'collect', return_value={'boot_count': 12}), \
|
||||
patch.object(os, 'fdopen', side_effect=inspect_fd), \
|
||||
patch.object(Path, 'open', new=inspect_path), \
|
||||
patch('sys.stdout', new_callable=io.StringIO):
|
||||
module.main()
|
||||
finally:
|
||||
os.umask(saved_umask)
|
||||
self.assertEqual(creation_modes, [0o600])
|
||||
self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600)
|
||||
self.assertEqual(json.loads(output.read_text()), {'boot_count': 12})
|
||||
|
||||
def test_output_refuses_existing_file(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
output = Path(directory) / 'diagnostics.json'
|
||||
output.write_text('existing controlled test data')
|
||||
with self.assertRaises(FileExistsError):
|
||||
module.write_private_output(output, '{"replacement":true}\n')
|
||||
self.assertEqual(output.read_text(), 'existing controlled test data')
|
||||
|
||||
@unittest.skipUnless(os.name == 'posix', 'POSIX symlink behavior')
|
||||
def test_output_refuses_symlink_without_touching_target(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
target = Path(directory) / 'target.json'
|
||||
target.write_text('existing controlled test data')
|
||||
output = Path(directory) / 'diagnostics.json'
|
||||
output.symlink_to(target)
|
||||
with self.assertRaises(FileExistsError):
|
||||
module.write_private_output(output, '{"replacement":true}\n')
|
||||
self.assertTrue(output.is_symlink())
|
||||
self.assertEqual(target.read_text(), 'existing controlled test data')
|
||||
|
||||
def test_locked_skips_kernel_body(self):
|
||||
commands = []
|
||||
def run(argv, **kwargs):
|
||||
|
||||
Reference in new issue
Block a user