amd-debug-tools 0.2.19__tar.gz → 0.2.20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. {amd_debug_tools-0.2.19/src/amd_debug_tools.egg-info → amd_debug_tools-0.2.20}/PKG-INFO +1 -1
  2. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/common.py +1 -1
  3. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/database.py +1 -5
  4. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/failures.py +12 -0
  5. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/kernel.py +31 -0
  6. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/prerequisites.py +31 -6
  7. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/wake.py +27 -20
  8. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20/src/amd_debug_tools.egg-info}/PKG-INFO +1 -1
  9. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug_tools.egg-info/SOURCES.txt +2 -0
  10. amd_debug_tools-0.2.20/src/amd_debug_tools.egg-info/scm_file_list.json +67 -0
  11. amd_debug_tools-0.2.20/src/amd_debug_tools.egg-info/scm_version.json +8 -0
  12. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_common.py +16 -0
  13. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_database.py +18 -0
  14. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_kernel.py +41 -0
  15. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_prerequisites.py +150 -0
  16. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_wake.py +36 -0
  17. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/.github/workflows/ci.yml +0 -0
  18. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/.github/workflows/mirror.yml +0 -0
  19. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/.gitignore +0 -0
  20. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/.pre-commit-config.yaml +0 -0
  21. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/.vscode/settings.json +0 -0
  22. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/LICENSE +0 -0
  23. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/Makefile +0 -0
  24. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/README.md +0 -0
  25. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/amd_bios.py +0 -0
  26. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/amd_pstate.py +0 -0
  27. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/amd_s2idle.py +0 -0
  28. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/amd_ttm.py +0 -0
  29. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/docs/amd-bios.md +0 -0
  30. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/docs/amd-pstate.md +0 -0
  31. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/docs/amd-s2idle.md +0 -0
  32. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/docs/amd-ttm.md +0 -0
  33. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/docs/in-tree.md +0 -0
  34. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/install_deps.py +0 -0
  35. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/psr.py +0 -0
  36. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/pyproject.toml +0 -0
  37. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/setup.cfg +0 -0
  38. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/__init__.py +0 -0
  39. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/acpi.py +0 -0
  40. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/bash/amd-s2idle +0 -0
  41. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/battery.py +0 -0
  42. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/bios.py +0 -0
  43. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/display.py +0 -0
  44. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/installer.py +0 -0
  45. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/power_rails.py +0 -0
  46. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/pstate.py +0 -0
  47. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/s2idle-hook +0 -0
  48. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/s2idle.py +0 -0
  49. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/sleep_report.py +0 -0
  50. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/templates/html +0 -0
  51. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/templates/md +0 -0
  52. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/templates/stdout +0 -0
  53. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/templates/txt +0 -0
  54. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/ttm.py +0 -0
  55. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug/validator.py +0 -0
  56. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug_tools.egg-info/dependency_links.txt +0 -0
  57. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug_tools.egg-info/entry_points.txt +0 -0
  58. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug_tools.egg-info/requires.txt +0 -0
  59. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/amd_debug_tools.egg-info/top_level.txt +0 -0
  60. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/launcher.py +0 -0
  61. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_acpi.py +0 -0
  62. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_batteries.py +0 -0
  63. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_bios.py +0 -0
  64. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_display.py +0 -0
  65. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_failures.py +0 -0
  66. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_installer.py +0 -0
  67. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_launcher.py +0 -0
  68. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_power_rails.py +0 -0
  69. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_pstate.py +0 -0
  70. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_s2idle.py +0 -0
  71. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_sleep_report.py +0 -0
  72. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_ttm.py +0 -0
  73. {amd_debug_tools-0.2.19 → amd_debug_tools-0.2.20}/src/test_validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amd-debug-tools
3
- Version: 0.2.19
3
+ Version: 0.2.20
4
4
  Summary: debug tools for AMD systems
5
5
  Author-email: Mario Limonciello <superm1@kernel.org>
6
6
  License-Expression: MIT
@@ -394,7 +394,7 @@ def read_msr(msr, cpu):
394
394
  """Read a Model-Specific Register (MSR) value from the CPU."""
395
395
  p = f"/dev/cpu/{cpu}/msr"
396
396
  if not os.path.exists(p) and is_root():
397
- os.system("modprobe msr")
397
+ subprocess.run(["/sbin/modprobe", "msr"], check=False)
398
398
  try:
399
399
  f = os.open(p, os.O_RDONLY)
400
400
  except OSError as exc:
@@ -44,12 +44,8 @@ class SleepDatabase:
44
44
  self.debug_cnt = 0
45
45
 
46
46
  if not dbf:
47
- # if we were packaged we would have a directory in /var/lib
48
47
  path = os.path.join("/", "var", "lib", "amd-s2idle")
49
- if not os.path.exists(path):
50
- path = os.path.join("/", "var", "local", "lib", "amd-s2idle")
51
- os.makedirs(path, exist_ok=True)
52
-
48
+ os.makedirs(path, mode=0o700, exist_ok=True)
53
49
  dbf = os.path.join(path, "data.db")
54
50
  new = not os.path.exists(dbf)
55
51
  self.db = sqlite3.connect(dbf)
@@ -653,3 +653,15 @@ class MissingAmdCaptureModule(S0i3Failure):
653
653
  "If the module is not available, disable the camera in the BIOS to prevent issues."
654
654
  )
655
655
  self.url = "https://gitlab.freedesktop.org/drm/amd/-/issues/4869"
656
+
657
+
658
+ class NpuIommu(S0i3Failure):
659
+ """IOMMU configuration incompatible with NPU"""
660
+
661
+ def __init__(self):
662
+ super().__init__()
663
+ self.description = "IOMMU configuration incompatible with NPU"
664
+ self.explanation = (
665
+ "The NPU requires a properly configured IOMMU to function correctly. "
666
+ "Ensure that the IOMMU is enabled and configured in the BIOS."
667
+ )
@@ -94,6 +94,30 @@ def get_kernel_command_line() -> str:
94
94
  return " ".join([x for x in cmdline.split() if not x.startswith(tuple(filtered))])
95
95
 
96
96
 
97
+ def redact_sensitive(text: str) -> str:
98
+ """Redact high-confidence sensitive tokens from captured log text.
99
+
100
+ Only obviously-sensitive patterns are scrubbed so that the remaining
101
+ content keeps its diagnostic value:
102
+ - MAC addresses
103
+ - UUIDs (e.g. LUKS/filesystem identifiers)
104
+ - key/secret/password/token assignments
105
+ """
106
+ text = re.sub(r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}", "<redacted-mac>", text)
107
+ text = re.sub(
108
+ r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
109
+ r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
110
+ "<redacted-uuid>",
111
+ text,
112
+ )
113
+ text = re.sub(
114
+ r"(?i)\b(key|secret|password|passwd|token)=\S+",
115
+ r"\1=<redacted>",
116
+ text,
117
+ )
118
+ return text
119
+
120
+
97
121
  def sscanf_bios_args(line):
98
122
  """Extracts the format string and arguments from a BIOS trace line"""
99
123
  if re.search(r"ex_trace_point", line):
@@ -128,6 +152,13 @@ def sscanf_bios_args(line):
128
152
  else:
129
153
  break
130
154
 
155
+ # Reject unexpected conversions or oversized field widths from
156
+ # untrusted firmware-supplied format strings.
157
+ if re.search(r"%(?![xXdD%])", format_string) or re.search(
158
+ r"%\d{4,}", format_string
159
+ ):
160
+ return None
161
+
131
162
  try:
132
163
  return format_string % tuple(converted_args)
133
164
  except TypeError:
@@ -20,7 +20,13 @@ import pyudev
20
20
 
21
21
  from amd_debug.wake import WakeIRQ
22
22
  from amd_debug.display import Display
23
- from amd_debug.kernel import get_kernel_log, SystemdLogger, CySystemdLogger, DmesgLogger
23
+ from amd_debug.kernel import (
24
+ get_kernel_log,
25
+ redact_sensitive,
26
+ SystemdLogger,
27
+ CySystemdLogger,
28
+ DmesgLogger,
29
+ )
24
30
  from amd_debug.common import (
25
31
  apply_prefix_wrapper,
26
32
  BIT,
@@ -67,6 +73,7 @@ from amd_debug.failures import (
67
73
  MissingThunderbolt,
68
74
  MissingXhciHcd,
69
75
  MSRFailure,
76
+ NpuIommu,
70
77
  RogAllyMcuPowerSave,
71
78
  RogAllyOldMcu,
72
79
  SleepModeWrong,
@@ -343,21 +350,39 @@ class PrerequisiteValidator(AmdTool):
343
350
 
344
351
  def check_amd_xdna(self):
345
352
  """Check for AMD XDNA driver"""
353
+ amdxdna_path = os.path.join("/", "sys", "module", "amdxdna")
354
+ driver_loaded = os.path.exists(amdxdna_path)
355
+ # No driver loaded means HW should be power gated, should be OK.
356
+ if not driver_loaded:
357
+ return True
358
+
359
+ # IOMMU disabled can cause problems with the NPU
360
+ found_iommu = False
361
+ for _dev in self.pyudev.list_devices(subsystem="iommu"):
362
+ found_iommu = True
363
+ break
346
364
  for device in self.pyudev.list_devices(subsystem="pci", PCI_CLASS="118000"):
347
365
  slot = device.properties["PCI_SLOT_NAME"]
348
366
  driver = device.properties.get("DRIVER")
349
367
  pci_id = device.properties.get("PCI_ID")
350
368
  if not pci_id.startswith("1022"):
351
369
  continue
352
- if not driver:
353
- self.db.record_prereq(f"NPU device in {slot} missing driver", "🚦")
354
- self.failures += [MissingDriver(slot)]
355
370
  p = os.path.join(device.sys_path, "fw_version")
356
371
  if os.path.exists(p):
357
372
  xdna_fw_version = read_file(p)
358
373
  self.db.record_prereq(
359
374
  f"NPU loaded with firmware {xdna_fw_version}", "✅"
360
375
  )
376
+ if not found_iommu and driver:
377
+ self.db.record_prereq("NPU is not supported with IOMMU disabled", "❌")
378
+ self.failures += [NpuIommu()]
379
+ return False
380
+ if not driver:
381
+ self.db.record_prereq(
382
+ f"NPU device in {slot} didn't bind a driver", "🚦"
383
+ )
384
+ self.failures += [MissingDriver(slot)]
385
+
361
386
  return True
362
387
 
363
388
  def check_amd_hsmp(self):
@@ -1158,7 +1183,7 @@ class PrerequisiteValidator(AmdTool):
1158
1183
  """Read CPUID using kernel userspace interface"""
1159
1184
  p = os.path.join("/", "dev", "cpu", f"{cpu}", "cpuid")
1160
1185
  if not os.path.exists(p):
1161
- os.system("modprobe cpuid")
1186
+ subprocess.run(["/sbin/modprobe", "cpuid"], check=False)
1162
1187
  with open(p, "rb") as f:
1163
1188
  position = (subleaf << 32) | leaf
1164
1189
  f.seek(position)
@@ -1432,7 +1457,7 @@ class PrerequisiteValidator(AmdTool):
1432
1457
  result = False
1433
1458
  if not result:
1434
1459
  self.db.record_prereq(Headers.BrokenPrerequisites, "🚫")
1435
- self.db.record_debug(self.kernel_log.get_full_log())
1460
+ self.db.record_debug(redact_sensitive(self.kernel_log.get_full_log()))
1436
1461
  self.db.sync()
1437
1462
  clear_temporary_message(len(msg))
1438
1463
  return result
@@ -75,29 +75,36 @@ class WakeIRQ:
75
75
 
76
76
  # "might" look like an ACPI device, try to follow it
77
77
  if not self.name and self.actions:
78
- p = os.path.join("/", "sys", "bus", "acpi", "devices", self.actions)
79
- if os.path.exists(p):
80
- for directory in os.listdir(p):
81
- if "physical_node" not in directory:
82
- continue
83
-
84
- for root, _dirs, files in os.walk(
85
- os.path.join(p, directory), followlinks=True
86
- ):
87
- if "name" in files:
88
- try:
89
- self.name = read_file(os.path.join(root, "name"))
90
- except (PermissionError, FileNotFoundError):
91
- pass
92
- t = os.path.join(root, "driver")
93
- if os.path.exists(t):
78
+ # self.actions is a free-form string supplied to request_irq() by
79
+ # the kernel, so restrict it to a single path component to avoid
80
+ # escaping the ACPI device directory via "/" or "..".
81
+ comp = os.path.basename(self.actions)
82
+ if comp and comp not in (".", ".."):
83
+ p = os.path.join("/", "sys", "bus", "acpi", "devices", comp)
84
+ if os.path.exists(p):
85
+ for directory in os.listdir(p):
86
+ if "physical_node" not in directory:
87
+ continue
88
+
89
+ for root, _dirs, files in os.walk(
90
+ os.path.join(p, directory), followlinks=False
91
+ ):
92
+ if "name" in files:
94
93
  try:
95
- self.driver = os.path.basename(os.readlink(t))
96
- except (PermissionError, FileNotFoundError, OSError):
94
+ self.name = read_file(os.path.join(root, "name"))
95
+ except (PermissionError, FileNotFoundError):
97
96
  pass
97
+ t = os.path.join(root, "driver")
98
+ if os.path.exists(t):
99
+ try:
100
+ self.driver = os.path.basename(
101
+ os.readlink(t)
102
+ )
103
+ except (PermissionError, FileNotFoundError, OSError):
104
+ pass
105
+ break
106
+ if self.name:
98
107
  break
99
- if self.name:
100
- break
101
108
 
102
109
  # If the name isn't descriptive try to guess further
103
110
  if self.driver and self.actions == self.name:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amd-debug-tools
3
- Version: 0.2.19
3
+ Version: 0.2.20
4
4
  Summary: debug tools for AMD systems
5
5
  Author-email: Mario Limonciello <superm1@kernel.org>
6
6
  License-Expression: MIT
@@ -66,4 +66,6 @@ src/amd_debug_tools.egg-info/SOURCES.txt
66
66
  src/amd_debug_tools.egg-info/dependency_links.txt
67
67
  src/amd_debug_tools.egg-info/entry_points.txt
68
68
  src/amd_debug_tools.egg-info/requires.txt
69
+ src/amd_debug_tools.egg-info/scm_file_list.json
70
+ src/amd_debug_tools.egg-info/scm_version.json
69
71
  src/amd_debug_tools.egg-info/top_level.txt
@@ -0,0 +1,67 @@
1
+ {
2
+ "files": [
3
+ ".pre-commit-config.yaml",
4
+ "README.md",
5
+ "amd_ttm.py",
6
+ "Makefile",
7
+ "amd_bios.py",
8
+ "LICENSE",
9
+ "pyproject.toml",
10
+ ".gitignore",
11
+ "amd_s2idle.py",
12
+ "install_deps.py",
13
+ "psr.py",
14
+ "amd_pstate.py",
15
+ ".vscode/settings.json",
16
+ "docs/amd-ttm.md",
17
+ "docs/in-tree.md",
18
+ "docs/amd-pstate.md",
19
+ "docs/amd-bios.md",
20
+ "docs/amd-s2idle.md",
21
+ "src/test_wake.py",
22
+ "src/test_validator.py",
23
+ "src/test_pstate.py",
24
+ "src/test_prerequisites.py",
25
+ "src/test_launcher.py",
26
+ "src/test_batteries.py",
27
+ "src/test_sleep_report.py",
28
+ "src/test_s2idle.py",
29
+ "src/test_power_rails.py",
30
+ "src/test_ttm.py",
31
+ "src/test_kernel.py",
32
+ "src/test_acpi.py",
33
+ "src/test_installer.py",
34
+ "src/test_database.py",
35
+ "src/test_bios.py",
36
+ "src/launcher.py",
37
+ "src/test_failures.py",
38
+ "src/test_common.py",
39
+ "src/test_display.py",
40
+ "src/amd_debug/wake.py",
41
+ "src/amd_debug/ttm.py",
42
+ "src/amd_debug/validator.py",
43
+ "src/amd_debug/__init__.py",
44
+ "src/amd_debug/pstate.py",
45
+ "src/amd_debug/kernel.py",
46
+ "src/amd_debug/failures.py",
47
+ "src/amd_debug/power_rails.py",
48
+ "src/amd_debug/sleep_report.py",
49
+ "src/amd_debug/bios.py",
50
+ "src/amd_debug/installer.py",
51
+ "src/amd_debug/display.py",
52
+ "src/amd_debug/s2idle.py",
53
+ "src/amd_debug/prerequisites.py",
54
+ "src/amd_debug/acpi.py",
55
+ "src/amd_debug/battery.py",
56
+ "src/amd_debug/database.py",
57
+ "src/amd_debug/s2idle-hook",
58
+ "src/amd_debug/common.py",
59
+ "src/amd_debug/bash/amd-s2idle",
60
+ "src/amd_debug/templates/txt",
61
+ "src/amd_debug/templates/md",
62
+ "src/amd_debug/templates/html",
63
+ "src/amd_debug/templates/stdout",
64
+ ".github/workflows/ci.yml",
65
+ ".github/workflows/mirror.yml"
66
+ ]
67
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "tag": "0.2.20",
3
+ "distance": 0,
4
+ "node": "g06e4e0a9a86ace73d23e06d646c6b8c139e84473",
5
+ "dirty": false,
6
+ "branch": "HEAD",
7
+ "node_date": "2026-07-10"
8
+ }
@@ -34,6 +34,7 @@ from amd_debug.common import (
34
34
  is_root,
35
35
  minimum_kernel,
36
36
  print_color,
37
+ read_msr,
37
38
  reboot,
38
39
  run_countdown,
39
40
  systemd_in_use,
@@ -233,6 +234,21 @@ class TestCommon(unittest.TestCase):
233
234
  mock_geteuid.assert_called_once()
234
235
  self.assertEqual(mock_geteuid.call_count, 1)
235
236
 
237
+ @patch("amd_debug.common.os.open", side_effect=OSError)
238
+ @patch("amd_debug.common.subprocess.run")
239
+ @patch("amd_debug.common.is_root", return_value=True)
240
+ @patch("amd_debug.common.os.path.exists", return_value=False)
241
+ def test_read_msr_loads_module(
242
+ self, mock_exists, mock_is_root, mock_run, mock_open_fd
243
+ ):
244
+ """Test read_msr loads the msr module via an absolute path and no shell"""
245
+ with self.assertRaises(PermissionError):
246
+ read_msr(0xC0010292, 0)
247
+ mock_run.assert_called_once_with(["/sbin/modprobe", "msr"], check=False)
248
+ # ensure the module load never went through a shell
249
+ _, kwargs = mock_run.call_args
250
+ self.assertNotIn("shell", kwargs)
251
+
236
252
  def test_get_log_priority(self):
237
253
  """Test get_log_priority works for expected values"""
238
254
  ret = get_log_priority(None)
@@ -7,6 +7,7 @@ This module contains unit tests for the datbase functions in the amd-debug-tools
7
7
  import unittest
8
8
 
9
9
  from datetime import datetime
10
+ from unittest.mock import patch, MagicMock
10
11
 
11
12
  from amd_debug.database import SleepDatabase
12
13
 
@@ -364,6 +365,23 @@ class TestSleepDatabase(unittest.TestCase):
364
365
  result = self.db.report_power_rails(timestamp)
365
366
  self.assertEqual(result, [])
366
367
 
368
+ def test_default_db_path_uses_var_lib_restrictive(self):
369
+ """Test the default database path is created securely in /var/lib"""
370
+ with patch("amd_debug.database.os.makedirs") as makedirs, patch(
371
+ "amd_debug.database.os.path.exists", return_value=False
372
+ ), patch("amd_debug.database.sqlite3.connect", return_value=MagicMock()):
373
+ SleepDatabase()
374
+
375
+ # Directory is created in /var/lib with a restrictive mode
376
+ makedirs.assert_called_once_with(
377
+ "/var/lib/amd-s2idle", mode=0o700, exist_ok=True
378
+ )
379
+
380
+ # The insecure /var/local fallback is never referenced
381
+ for call in makedirs.call_args_list:
382
+ for arg in call.args:
383
+ self.assertNotIn("/var/local", str(arg))
384
+
367
385
  def test_schema_migration_v1_to_v2(self):
368
386
  """Test database migration from schema v1 to v2"""
369
387
  # Create a new database (will be at v2)
@@ -14,6 +14,7 @@ import logging
14
14
  from amd_debug.kernel import (
15
15
  sscanf_bios_args,
16
16
  get_kernel_command_line,
17
+ redact_sensitive,
17
18
  DmesgLogger,
18
19
  InputFile,
19
20
  KernelLogger,
@@ -67,6 +68,31 @@ class TestKernelLog(unittest.TestCase):
67
68
  result = get_kernel_command_line()
68
69
  self.assertEqual(result, expected_output)
69
70
 
71
+ def test_redact_sensitive(self):
72
+ """Test redact_sensitive function"""
73
+
74
+ # MAC addresses are redacted
75
+ result = redact_sensitive("wlan0: link up 00:11:22:33:44:55 detected")
76
+ self.assertEqual(result, "wlan0: link up <redacted-mac> detected")
77
+
78
+ # UUIDs are redacted
79
+ result = redact_sensitive(
80
+ "cryptsetup: 12345678-90ab-cdef-1234-567890abcdef mounted"
81
+ )
82
+ self.assertEqual(result, "cryptsetup: <redacted-uuid> mounted")
83
+
84
+ # key/secret/password/token assignments are redacted
85
+ self.assertEqual(
86
+ redact_sensitive("luks key=abcd1234"), "luks key=<redacted>"
87
+ )
88
+ self.assertEqual(
89
+ redact_sensitive("token=deadbeef"), "token=<redacted>"
90
+ )
91
+
92
+ # Ordinary log text is preserved
93
+ line = "amd_pmc AMDI0009:00: SMU idlemask s0i3: 0x1234abcd"
94
+ self.assertEqual(redact_sensitive(line), line)
95
+
70
96
  def test_sscanf_bios_args(self):
71
97
  """Test sscanf_bios_args function"""
72
98
 
@@ -338,6 +364,21 @@ class TestSscanfBiosArgsEdge(unittest.TestCase):
338
364
  """ev_queue_notify_reques without ': ' returns None"""
339
365
  self.assertIsNone(sscanf_bios_args("ev_queue_notify_reques nothing"))
340
366
 
367
+ def test_ex_trace_args_oversized_width(self):
368
+ """An oversized field width is rejected instead of allocating memory"""
369
+ line = 'ex_trace_args: "%2147483646d", 1, 0, 0, 0, 0, 0'
370
+ self.assertIsNone(sscanf_bios_args(line))
371
+
372
+ def test_ex_trace_args_unexpected_conversion(self):
373
+ """A conversion outside the whitelist is rejected"""
374
+ line = 'ex_trace_args: "value: %s", 1, 0, 0, 0, 0, 0'
375
+ self.assertIsNone(sscanf_bios_args(line))
376
+
377
+ def test_ex_trace_args_valid_conversion(self):
378
+ """A whitelisted conversion is still applied normally"""
379
+ line = 'ex_trace_args: "%x", 1234'
380
+ self.assertEqual(sscanf_bios_args(line), "1234")
381
+
341
382
 
342
383
  class TestGetKernelLog(unittest.TestCase):
343
384
  """Test get_kernel_log provider selection"""
@@ -98,6 +98,131 @@ class TestPrerequisiteValidator(unittest.TestCase):
98
98
  self.assertFalse(result)
99
99
  self.assertTrue(any(isinstance(f, AmdHsmpBug) for f in self.validator.failures))
100
100
 
101
+ @patch("amd_debug.prerequisites.os.path.exists")
102
+ def test_check_amd_xdna_no_driver(self, mock_exists):
103
+ """Test check_amd_xdna when driver is not loaded"""
104
+ mock_exists.return_value = False
105
+ result = self.validator.check_amd_xdna()
106
+ self.assertTrue(result)
107
+ self.mock_db.record_prereq.assert_not_called()
108
+
109
+ @patch("amd_debug.prerequisites.os.path.exists")
110
+ def test_check_amd_xdna_no_iommu(self, mock_exists):
111
+ """Test check_amd_xdna when IOMMU is disabled"""
112
+ mock_exists.side_effect = lambda p: True if "amdxdna" in p else False
113
+
114
+ def mock_list_devices(**kwargs):
115
+ if kwargs.get("subsystem") == "iommu":
116
+ return []
117
+ if kwargs.get("subsystem") == "pci":
118
+ return [
119
+ MagicMock(
120
+ properties={
121
+ "PCI_SLOT_NAME": "0000:01:00.0",
122
+ "PCI_ID": "1022:1234",
123
+ "DRIVER": "amdxdna",
124
+ }
125
+ )
126
+ ]
127
+ return []
128
+
129
+ self.mock_pyudev.list_devices.side_effect = mock_list_devices
130
+
131
+ result = self.validator.check_amd_xdna()
132
+ self.assertFalse(result)
133
+ self.assertTrue(any(isinstance(f, NpuIommu) for f in self.validator.failures))
134
+ self.mock_db.record_prereq.assert_called_with(
135
+ "NPU is not supported with IOMMU disabled", "❌"
136
+ )
137
+
138
+ @patch("amd_debug.prerequisites.os.path.exists")
139
+ def test_check_amd_xdna_missing_driver(self, mock_exists):
140
+ """Test check_amd_xdna when NPU is found but no driver is bound"""
141
+ mock_exists.side_effect = lambda p: True if "amdxdna" in p else False
142
+
143
+ def mock_list_devices(**kwargs):
144
+ if kwargs.get("subsystem") == "iommu":
145
+ return [MagicMock()]
146
+ if kwargs.get("subsystem") == "pci":
147
+ return [
148
+ MagicMock(
149
+ properties={
150
+ "PCI_SLOT_NAME": "0000:01:00.0",
151
+ "PCI_ID": "1022:1234",
152
+ "DRIVER": None,
153
+ }
154
+ )
155
+ ]
156
+ return []
157
+
158
+ self.mock_pyudev.list_devices.side_effect = mock_list_devices
159
+
160
+ result = self.validator.check_amd_xdna()
161
+ self.assertTrue(result)
162
+ self.assertTrue(
163
+ any(isinstance(f, MissingDriver) for f in self.validator.failures)
164
+ )
165
+ self.mock_db.record_prereq.assert_called_with(
166
+ "NPU device in 0000:01:00.0 didn't bind a driver", "🚦"
167
+ )
168
+
169
+ @patch("amd_debug.prerequisites.read_file")
170
+ @patch("amd_debug.prerequisites.os.path.exists")
171
+ def test_check_amd_xdna_success(self, mock_exists, mock_read_file):
172
+ """Test check_amd_xdna when driver is loaded, NPU is bound, and IOMMU is enabled"""
173
+ mock_exists.side_effect = lambda p: True
174
+ mock_read_file.return_value = "1.0.0"
175
+
176
+ def mock_list_devices(**kwargs):
177
+ if kwargs.get("subsystem") == "iommu":
178
+ return [MagicMock()]
179
+ if kwargs.get("subsystem") == "pci":
180
+ return [
181
+ MagicMock(
182
+ sys_path="/sys/devices/pci",
183
+ properties={
184
+ "PCI_SLOT_NAME": "0000:01:00.0",
185
+ "PCI_ID": "1022:1234",
186
+ "DRIVER": "amdxdna",
187
+ },
188
+ )
189
+ ]
190
+ return []
191
+
192
+ self.mock_pyudev.list_devices.side_effect = mock_list_devices
193
+
194
+ result = self.validator.check_amd_xdna()
195
+ self.assertTrue(result)
196
+ self.mock_db.record_prereq.assert_called_with(
197
+ "NPU loaded with firmware 1.0.0", "✅"
198
+ )
199
+
200
+ @patch("amd_debug.prerequisites.os.path.exists")
201
+ def test_check_amd_xdna_non_amd(self, mock_exists):
202
+ """Test check_amd_xdna when the NPU is not an AMD device"""
203
+ mock_exists.side_effect = lambda p: True if "amdxdna" in p else False
204
+
205
+ def mock_list_devices(**kwargs):
206
+ if kwargs.get("subsystem") == "iommu":
207
+ return [MagicMock()]
208
+ if kwargs.get("subsystem") == "pci":
209
+ return [
210
+ MagicMock(
211
+ properties={
212
+ "PCI_SLOT_NAME": "0000:01:00.0",
213
+ "PCI_ID": "8086:abcd",
214
+ "DRIVER": "amdxdna",
215
+ }
216
+ )
217
+ ]
218
+ return []
219
+
220
+ self.mock_pyudev.list_devices.side_effect = mock_list_devices
221
+
222
+ result = self.validator.check_amd_xdna()
223
+ self.assertTrue(result)
224
+ self.mock_db.record_prereq.assert_not_called()
225
+
101
226
  def test_check_amd_pmc_no_driver(self):
102
227
  """Test check_amd_pmc with no driver"""
103
228
  self.mock_pyudev.list_devices.return_value = []
@@ -592,6 +717,31 @@ class TestPrerequisiteValidator(unittest.TestCase):
592
717
  "Unable to check CPU topology: cpuid kernel module not loaded", "❌"
593
718
  )
594
719
 
720
+ @patch("amd_debug.prerequisites.subprocess.run")
721
+ @patch("builtins.open", new_callable=mock_open)
722
+ @patch("amd_debug.prerequisites.read_file")
723
+ @patch("amd_debug.prerequisites.os.path.exists")
724
+ def test_check_cpu_loads_cpuid_module(
725
+ self, mock_path_exists, mock_read_file, mock_file_open, mock_run
726
+ ):
727
+ """Test read_cpuid loads the cpuid module via an absolute path and no shell"""
728
+ self.validator.cpu_family = 0x19
729
+ self.validator.cpu_model = 0x74
730
+ mock_read_file.return_value = "31" # kernel_max = 31 (32 cores)
731
+ # cpuid device node missing so the module load path is exercised
732
+ mock_path_exists.return_value = False
733
+ mock_file_open.return_value.read.side_effect = [
734
+ struct.pack("4I", 0, 0, 0x100, 0), # subleaf 0: level_type = 1
735
+ struct.pack("4I", 0, 0, 0x400, 0), # subleaf 1: level_type = 4 (socket)
736
+ struct.pack("4I", 0, 16, 0, 0), # subleaf 1: cpu_count = 16
737
+ ]
738
+ result = self.validator.check_cpu()
739
+ self.assertTrue(result)
740
+ mock_run.assert_any_call(["/sbin/modprobe", "cpuid"], check=False)
741
+ # ensure the module load never went through a shell
742
+ for _, kwargs in mock_run.call_args_list:
743
+ self.assertNotIn("shell", kwargs)
744
+
595
745
  @patch("builtins.open", side_effect=PermissionError)
596
746
  @patch("amd_debug.prerequisites.read_file")
597
747
  @patch("amd_debug.prerequisites.os.path.exists")
@@ -492,6 +492,42 @@ class TestWakeIRQ(unittest.TestCase):
492
492
  irq = WakeIRQ(80)
493
493
  self.assertEqual(irq.name, "")
494
494
 
495
+ @patch("amd_debug.wake.read_file")
496
+ @patch("os.path.exists")
497
+ @patch("os.listdir")
498
+ @patch("os.walk")
499
+ def test_wake_irq_acpi_path_traversal(
500
+ self, mock_os_walk, mock_os_listdir, mock_os_path_exists, mock_read_file
501
+ ):
502
+ """A traversal sequence in actions must not escape the ACPI device dir"""
503
+ mock_read_file.side_effect = lambda path: {
504
+ "/sys/kernel/irq/82/chip_name": "",
505
+ "/sys/kernel/irq/82/actions": "../../foo",
506
+ "/sys/kernel/irq/82/wakeup": "enabled",
507
+ }.get(path, "")
508
+
509
+ checked = []
510
+
511
+ def exists_side_effect(path):
512
+ checked.append(path)
513
+ return False
514
+
515
+ mock_os_path_exists.side_effect = exists_side_effect
516
+
517
+ irq = WakeIRQ(82)
518
+
519
+ # The action name must be reduced to its basename before being joined
520
+ # under the ACPI devices directory, so no ".." component is ever used.
521
+ acpi_checks = [
522
+ p for p in checked if p.startswith("/sys/bus/acpi/devices")
523
+ ]
524
+ self.assertIn("/sys/bus/acpi/devices/foo", acpi_checks)
525
+ for p in acpi_checks:
526
+ self.assertNotIn("..", p)
527
+ # os.walk must never run since the sanitised path does not exist.
528
+ mock_os_walk.assert_not_called()
529
+ self.assertEqual(irq.name, "")
530
+
495
531
  def test_wake_irq_str(self):
496
532
  """Test __str__ of WakeIRQ"""
497
533
  with patch("amd_debug.wake.read_file", side_effect=FileNotFoundError()), \