acrionix-cli 0.4.0__py3-none-any.whl
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.
- acrionix/__init__.py +3 -0
- acrionix/__main__.py +4 -0
- acrionix/adb.py +136 -0
- acrionix/cli.py +281 -0
- acrionix/models.py +54 -0
- acrionix/parsers/__init__.py +12 -0
- acrionix/parsers/crashes.py +222 -0
- acrionix/parsers/selinux.py +195 -0
- acrionix/parsers/tree.py +74 -0
- acrionix/parsers/vintf.py +107 -0
- acrionix/patcher.py +260 -0
- acrionix_cli-0.4.0.dist-info/METADATA +102 -0
- acrionix_cli-0.4.0.dist-info/RECORD +17 -0
- acrionix_cli-0.4.0.dist-info/WHEEL +5 -0
- acrionix_cli-0.4.0.dist-info/entry_points.txt +2 -0
- acrionix_cli-0.4.0.dist-info/licenses/LICENSE +17 -0
- acrionix_cli-0.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Iterable
|
|
5
|
+
|
|
6
|
+
from acrionix.models import Finding
|
|
7
|
+
|
|
8
|
+
INIT_EXIT_RE = re.compile(
|
|
9
|
+
r"init:\s+Service\s+'(?P<svc>[^']+)'\s+\(pid\s+(?P<pid>\d+)\)\s+exited\s+with\s+status\s+(?P<status>-?\d+)"
|
|
10
|
+
)
|
|
11
|
+
INIT_CRASH_LOOP_RE = re.compile(
|
|
12
|
+
r"init:\s+Service\s+'(?P<svc>[^']+)'.*?crashed\s+(?P<count>\d+)\s+times"
|
|
13
|
+
)
|
|
14
|
+
FW_LOAD_ERR_RE = re.compile(
|
|
15
|
+
r"Direct firmware load for\s+(?P<blob>[^\s]+)\s+failed with error\s+(?P<err>-?\d+)"
|
|
16
|
+
)
|
|
17
|
+
TOMBSTONE_EXEC_RE = re.compile(r"^(?:Executable|Cmdline):\s+(?P<exe>\S+)")
|
|
18
|
+
TOMBSTONE_SIG_RE = re.compile(
|
|
19
|
+
r"signal\s+(?P<sig>\d+)\s+\((?P<signame>[A-Z0-9_]+)\),\s+code\s+\d+\s+\((?P<code>[A-Z0-9_]+)\),\s+fault addr\s+(?P<addr>0x[0-9a-fA-F]+)"
|
|
20
|
+
)
|
|
21
|
+
TOMBSTONE_FRAME_RE = re.compile(
|
|
22
|
+
r"#(?P<idx>0\d)\s+pc\s+(?P<pc>[0-9a-fA-F]+)\s+(?P<lib>[^\s]+)(?:\s+\((?P<sym>[^)]+)\))?"
|
|
23
|
+
)
|
|
24
|
+
OPEX_REBOOT_RE = re.compile(
|
|
25
|
+
r"(?:reboot reason:|Restarting system with command)\s+'?(?P<reason>opex_incremental_disable|dm-verity[^'\s]*|fs-mgr[^'\s]*|rescue|bootloader)'?",
|
|
26
|
+
re.IGNORECASE,
|
|
27
|
+
)
|
|
28
|
+
GSID_AVB_ERR_RE = re.compile(r"gsid\s*:\s*(?:invalid AVB footer|Failed to extract AVB public key)")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def parse_crashes(lines: Iterable[str]) -> list[Finding]:
|
|
32
|
+
raw_lines = [line.rstrip("\n") for line in lines]
|
|
33
|
+
findings: list[Finding] = []
|
|
34
|
+
|
|
35
|
+
fw_blobs: list[tuple[str, str, str]] = []
|
|
36
|
+
for line in raw_lines:
|
|
37
|
+
fm = FW_LOAD_ERR_RE.search(line)
|
|
38
|
+
if fm:
|
|
39
|
+
fw_blobs.append((fm.group("blob"), fm.group("err"), line.strip()))
|
|
40
|
+
|
|
41
|
+
# 1. Init service crashes (+ correlated firmware load failure)
|
|
42
|
+
seen_svcs: set[str] = set()
|
|
43
|
+
for line in raw_lines:
|
|
44
|
+
im = INIT_EXIT_RE.search(line)
|
|
45
|
+
if not im:
|
|
46
|
+
continue
|
|
47
|
+
svc = im.group("svc")
|
|
48
|
+
status = int(im.group("status"))
|
|
49
|
+
if status == 0 or svc in seen_svcs:
|
|
50
|
+
continue
|
|
51
|
+
seen_svcs.add(svc)
|
|
52
|
+
|
|
53
|
+
matched_fw = fw_blobs[0] if fw_blobs else None
|
|
54
|
+
if matched_fw:
|
|
55
|
+
blob, err, fw_line = matched_fw
|
|
56
|
+
findings.append(
|
|
57
|
+
Finding(
|
|
58
|
+
category="firmware",
|
|
59
|
+
severity="critical",
|
|
60
|
+
title=f"Init crash ({svc}) caused by missing firmware blob: {blob}",
|
|
61
|
+
summary=(
|
|
62
|
+
f"Service '{svc}' exited with status {status} after kernel firmware loader "
|
|
63
|
+
f"returned error {err} for '{blob}'. Copy the blob via PRODUCT_COPY_FILES "
|
|
64
|
+
"and delay daemon start until vendor firmware is mounted."
|
|
65
|
+
),
|
|
66
|
+
partition="partition: /vendor/firmware",
|
|
67
|
+
target_file="device/oem/common/device.mk",
|
|
68
|
+
confidence=0.99,
|
|
69
|
+
raw_lines=(fw_line, line.strip()),
|
|
70
|
+
metadata={
|
|
71
|
+
"service": svc,
|
|
72
|
+
"exit_status": status,
|
|
73
|
+
"firmware_blob": blob,
|
|
74
|
+
"kernel_error": err,
|
|
75
|
+
},
|
|
76
|
+
security_note="Ensures firmware blob is packaged in /vendor/firmware before daemon init.",
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
else:
|
|
80
|
+
findings.append(
|
|
81
|
+
Finding(
|
|
82
|
+
category="init",
|
|
83
|
+
severity="high",
|
|
84
|
+
title=f"Init service exited with status {status}: {svc}",
|
|
85
|
+
summary=(
|
|
86
|
+
f"Service '{svc}' exited abnormally (status {status}). Review its init.rc "
|
|
87
|
+
"class trigger, user/group permissions, and required device nodes."
|
|
88
|
+
),
|
|
89
|
+
partition="partition: /vendor/etc/init",
|
|
90
|
+
target_file="device/oem/common/init.vendor.rc",
|
|
91
|
+
confidence=0.92,
|
|
92
|
+
raw_lines=(line.strip(),),
|
|
93
|
+
metadata={"service": svc, "exit_status": status},
|
|
94
|
+
security_note="Adds late_start ordering guard to prevent early-boot restart loops.",
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Standalone firmware errors when no init exit line was present
|
|
99
|
+
if fw_blobs and not seen_svcs:
|
|
100
|
+
blob, err, fw_line = fw_blobs[0]
|
|
101
|
+
findings.append(
|
|
102
|
+
Finding(
|
|
103
|
+
category="firmware",
|
|
104
|
+
severity="high",
|
|
105
|
+
title=f"Kernel firmware load failed ({err}): {blob}",
|
|
106
|
+
summary=(
|
|
107
|
+
f"Kernel failed to load firmware '{blob}' with error {err}. "
|
|
108
|
+
"Verify the blob is declared in PRODUCT_COPY_FILES under $(TARGET_COPY_OUT_VENDOR)/firmware."
|
|
109
|
+
),
|
|
110
|
+
partition="partition: /vendor/firmware",
|
|
111
|
+
target_file="device/oem/common/device.mk",
|
|
112
|
+
confidence=0.96,
|
|
113
|
+
raw_lines=(fw_line,),
|
|
114
|
+
metadata={"firmware_blob": blob, "kernel_error": err},
|
|
115
|
+
security_note="Adds missing vendor firmware blob packaging entry.",
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# 2. Native Tombstone crash parser (SIGSEGV / SIGABRT + top stack frames)
|
|
120
|
+
exe_name: str | None = None
|
|
121
|
+
sig_info: tuple[str, str, str] | None = None
|
|
122
|
+
frames: list[str] = []
|
|
123
|
+
tomb_raw: list[str] = []
|
|
124
|
+
for line in raw_lines:
|
|
125
|
+
em = TOMBSTONE_EXEC_RE.search(line)
|
|
126
|
+
if em and not exe_name:
|
|
127
|
+
exe_name = em.group("exe")
|
|
128
|
+
tomb_raw.append(line.strip())
|
|
129
|
+
sm = TOMBSTONE_SIG_RE.search(line)
|
|
130
|
+
if sm and not sig_info:
|
|
131
|
+
sig_info = (sm.group("signame"), sm.group("code"), sm.group("addr"))
|
|
132
|
+
tomb_raw.append(line.strip())
|
|
133
|
+
fm = TOMBSTONE_FRAME_RE.search(line)
|
|
134
|
+
if fm and len(frames) < 4:
|
|
135
|
+
sym = f" ({fm.group('sym')})" if fm.group("sym") else ""
|
|
136
|
+
frames.append(f"#{fm.group('idx')} {fm.group('lib')}{sym}")
|
|
137
|
+
tomb_raw.append(line.strip())
|
|
138
|
+
|
|
139
|
+
if sig_info:
|
|
140
|
+
signame, code, addr = sig_info
|
|
141
|
+
target_bin = exe_name or "native binary"
|
|
142
|
+
top_frame = frames[0] if frames else "unknown frame"
|
|
143
|
+
findings.append(
|
|
144
|
+
Finding(
|
|
145
|
+
category="tombstone",
|
|
146
|
+
severity="critical",
|
|
147
|
+
title=f"Native crash {signame} ({code}) in {target_bin}",
|
|
148
|
+
summary=(
|
|
149
|
+
f"Process '{target_bin}' crashed with {signame} ({code}, fault addr {addr}) "
|
|
150
|
+
f"at {top_frame}. Inspect null-pointer dereference or ABI mismatch in the vendor library."
|
|
151
|
+
),
|
|
152
|
+
partition="partition: /vendor/lib64" if "/vendor/" in target_bin else "partition: /system",
|
|
153
|
+
target_file=target_bin,
|
|
154
|
+
confidence=0.97,
|
|
155
|
+
raw_lines=tuple(tomb_raw[:6]),
|
|
156
|
+
metadata={
|
|
157
|
+
"executable": target_bin,
|
|
158
|
+
"signal": signame,
|
|
159
|
+
"code": code,
|
|
160
|
+
"fault_addr": addr,
|
|
161
|
+
"frames": frames,
|
|
162
|
+
},
|
|
163
|
+
security_note="Native crash triaged; verify symbolicated library matches vendor VNDK/AIDL ABI.",
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# 3. DSU / GSI & OEM first-boot reboot loops (e.g. ColorOS opex_incremental_disable)
|
|
168
|
+
seen_reboots: set[str] = set()
|
|
169
|
+
for line in raw_lines:
|
|
170
|
+
om = OPEX_REBOOT_RE.search(line)
|
|
171
|
+
if om:
|
|
172
|
+
reason = om.group("reason")
|
|
173
|
+
if reason in seen_reboots:
|
|
174
|
+
continue
|
|
175
|
+
seen_reboots.add(reason)
|
|
176
|
+
if reason.lower() == "opex_incremental_disable":
|
|
177
|
+
summary = (
|
|
178
|
+
"Oplus/ColorOS triggered a first-boot reboot ('opex_incremental_disable') after "
|
|
179
|
+
"booting userspace. Under Android DSU with one_shot_boot=1, this reboot disables "
|
|
180
|
+
"the DSU slot and falls back to the host ROM. Enable sticky DSU mode (disable one_shot_boot) "
|
|
181
|
+
"so ColorOS completes its second boot."
|
|
182
|
+
)
|
|
183
|
+
sec_note = "Run `gsi_tool enable` (sticky mode, without -s) or disable one-shot in DSU Sideloader."
|
|
184
|
+
else:
|
|
185
|
+
summary = f"System initiated abnormal reboot with reason '{reason}'."
|
|
186
|
+
sec_note = "Check bootloader vbmeta/dm-verity flags and first-stage fstab mounts."
|
|
187
|
+
findings.append(
|
|
188
|
+
Finding(
|
|
189
|
+
category="dsu",
|
|
190
|
+
severity="critical",
|
|
191
|
+
title=f"Unexpected boot-time reboot trigger: {reason}",
|
|
192
|
+
summary=summary,
|
|
193
|
+
partition="partition: /metadata/gsi/dsu",
|
|
194
|
+
target_file="/metadata/gsi/dsu/one_shot_boot",
|
|
195
|
+
confidence=0.98,
|
|
196
|
+
raw_lines=(line.strip(),),
|
|
197
|
+
metadata={"reboot_reason": reason},
|
|
198
|
+
security_note=sec_note,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
avb_errs = [line.strip() for line in raw_lines if GSID_AVB_ERR_RE.search(line)]
|
|
203
|
+
if avb_errs:
|
|
204
|
+
findings.append(
|
|
205
|
+
Finding(
|
|
206
|
+
category="dsu",
|
|
207
|
+
severity="medium",
|
|
208
|
+
title="DSU/GSI image has no valid AVB footer",
|
|
209
|
+
summary=(
|
|
210
|
+
"gsid reported 'invalid AVB footer' on an installed DSU partition. "
|
|
211
|
+
"Unsigned GSI/OEM ports require androidboot.veritymode=disabled (unlocked bootloader / disabled vbmeta)."
|
|
212
|
+
),
|
|
213
|
+
partition="partition: /data/gsi/dsu",
|
|
214
|
+
target_file="/proc/bootconfig",
|
|
215
|
+
confidence=0.94,
|
|
216
|
+
raw_lines=tuple(avb_errs[:3]),
|
|
217
|
+
metadata={"avb_footer_missing": True},
|
|
218
|
+
security_note="Confirm vbmeta verification is disabled before booting unsigned DSU images.",
|
|
219
|
+
)
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
return findings
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections import defaultdict
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Iterable
|
|
7
|
+
|
|
8
|
+
from acrionix.models import Finding
|
|
9
|
+
|
|
10
|
+
AVC_RE = re.compile(
|
|
11
|
+
r"avc:\s+denied\s+\{\s*(?P<perms>[^}]+?)\s*\}.*?"
|
|
12
|
+
r"scontext=u:r:(?P<scontext>[a-zA-Z0-9_]+):s0(?::[^\s]+)?.*?"
|
|
13
|
+
r"tcontext=u:(?:r|object_r):(?P<tcontext>[a-zA-Z0-9_]+):s0(?::[^\s]+)?.*?"
|
|
14
|
+
r"tclass=(?P<tclass>[a-zA-Z0-9_]+)"
|
|
15
|
+
)
|
|
16
|
+
COMM_RE = re.compile(r'comm="([^"]+)"')
|
|
17
|
+
PERMISSIVE_RE = re.compile(r"\bpermissive=([01])\b")
|
|
18
|
+
PERMISSIVE_TE_RE = re.compile(r"^\s*permissive\s+([a-zA-Z0-9_]+)\s*;", re.MULTILINE)
|
|
19
|
+
|
|
20
|
+
# Common AOSP neverallow danger pairs that must be flagged rather than blindly allowed
|
|
21
|
+
NEVERALLOW_GUARD: list[tuple[str, str, str, str]] = [
|
|
22
|
+
("untrusted_app", "system_data_file", "file", "AOSP neverallow forbids untrusted_app direct access to system_data_file"),
|
|
23
|
+
("untrusted_app", "block_device", "blk_file", "AOSP neverallow forbids apps accessing raw block devices"),
|
|
24
|
+
("untrusted_app", "selinuxfs", "file", "AOSP CTS neverallow forbids untrusted_app reading selinuxfs (typically app anti-root probe)"),
|
|
25
|
+
("untrusted_app", "proc_kallsyms", "file", "AOSP CTS neverallow forbids untrusted_app inspecting kernel symbols (proc_kallsyms)"),
|
|
26
|
+
("untrusted_app", "proc_modules", "file", "AOSP CTS neverallow forbids untrusted_app inspecting loaded kernel modules (proc_modules)"),
|
|
27
|
+
("untrusted_app", "proc_keys", "file", "AOSP CTS neverallow forbids untrusted_app inspecting kernel keyring (proc_keys)"),
|
|
28
|
+
("untrusted_app", "proc_bootconfig", "file", "AOSP CTS neverallow forbids untrusted_app reading bootconfig (typically anti-tamper probe)"),
|
|
29
|
+
("untrusted_app", "cache_file", "lnk_file", "AOSP neverallow restricts untrusted_app traversing /cache symlinks"),
|
|
30
|
+
("hal_", "system_file", "file", "Treble neverallow restricts vendor HALs from writing/executing arbitrary system_file"),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
RW_FILE_SET = {"open", "read", "write", "getattr", "ioctl", "lock", "append", "map"}
|
|
34
|
+
R_FILE_SET = {"open", "read", "getattr", "ioctl", "lock", "map"}
|
|
35
|
+
R_DIR_SET = {"open", "read", "getattr", "search", "ioctl"}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def synthesize_te_rule(scontext: str, tcontext: str, tclass: str, perms: set[str]) -> tuple[str, str]:
|
|
39
|
+
"""Convert raw AVC tuple into idiomatic least-privilege AOSP .te rule or macro."""
|
|
40
|
+
if tclass == "binder" and "call" in perms:
|
|
41
|
+
if tcontext in ("servicemanager", "hwservicemanager", "vndservicemanager"):
|
|
42
|
+
return (
|
|
43
|
+
f"binder_call({scontext}, {tcontext})",
|
|
44
|
+
"Idiomatic AOSP binder_call() macro used instead of raw allow statement.",
|
|
45
|
+
)
|
|
46
|
+
return (
|
|
47
|
+
f"binder_call({scontext}, {tcontext})",
|
|
48
|
+
"Scoped IPC binder_call() macro synthesized.",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if tclass == "file":
|
|
52
|
+
if perms.issubset(R_FILE_SET) and len(perms) >= 3:
|
|
53
|
+
return (
|
|
54
|
+
f"allow {scontext} {tcontext}:file r_file_perms;",
|
|
55
|
+
"Collapsed permissions into AOSP r_file_perms macro.",
|
|
56
|
+
)
|
|
57
|
+
if perms.issubset(RW_FILE_SET) and "write" in perms and len(perms) >= 3:
|
|
58
|
+
return (
|
|
59
|
+
f"allow {scontext} {tcontext}:file rw_file_perms;",
|
|
60
|
+
"Collapsed permissions into AOSP rw_file_perms macro.",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if tclass == "dir" and perms.issubset(R_DIR_SET) and len(perms) >= 3:
|
|
64
|
+
return (
|
|
65
|
+
f"allow {scontext} {tcontext}:dir r_dir_perms;",
|
|
66
|
+
"Collapsed permissions into AOSP r_dir_perms macro.",
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
perm_str = " ".join(sorted(perms))
|
|
70
|
+
if len(perms) == 1:
|
|
71
|
+
rule = f"allow {scontext} {tcontext}:{tclass} {perm_str};"
|
|
72
|
+
else:
|
|
73
|
+
rule = f"allow {scontext} {tcontext}:{tclass} {{ {perm_str} }};"
|
|
74
|
+
return (rule, f"Least-privilege rule scoped strictly to {{{perm_str}}} on {tclass}.")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def check_neverallow_risk(scontext: str, tcontext: str, tclass: str, perms: set[str]) -> str | None:
|
|
78
|
+
if "write" in perms and tcontext == "system_file" and scontext.startswith("hal_"):
|
|
79
|
+
return "Treble neverallow: vendor HAL cannot write to system_file."
|
|
80
|
+
for sc_prefix, tc_match, cls_match, reason in NEVERALLOW_GUARD:
|
|
81
|
+
if scontext.startswith(sc_prefix) and tcontext == tc_match and tclass == cls_match:
|
|
82
|
+
if tclass == "file" and "write" not in perms and sc_prefix == "hal_":
|
|
83
|
+
continue
|
|
84
|
+
return reason
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def parse_selinux_denials(lines: Iterable[str]) -> list[Finding]:
|
|
89
|
+
grouped: dict[tuple[str, str, str], dict[str, object]] = defaultdict(
|
|
90
|
+
lambda: {"perms": set(), "comms": set(), "lines": [], "permissive": False}
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
for raw in lines:
|
|
94
|
+
m = AVC_RE.search(raw)
|
|
95
|
+
if not m:
|
|
96
|
+
continue
|
|
97
|
+
scontext = m.group("scontext")
|
|
98
|
+
tcontext = m.group("tcontext")
|
|
99
|
+
tclass = m.group("tclass")
|
|
100
|
+
perms = set(m.group("perms").split())
|
|
101
|
+
entry = grouped[(scontext, tcontext, tclass)]
|
|
102
|
+
entry["perms"].update(perms) # type: ignore[union-attr]
|
|
103
|
+
comm_m = COMM_RE.search(raw)
|
|
104
|
+
if comm_m:
|
|
105
|
+
entry["comms"].add(comm_m.group(1)) # type: ignore[union-attr]
|
|
106
|
+
perm_m = PERMISSIVE_RE.search(raw)
|
|
107
|
+
if perm_m and perm_m.group(1) == "1":
|
|
108
|
+
entry["permissive"] = True
|
|
109
|
+
raw_list: list[str] = entry["lines"] # type: ignore[assignment]
|
|
110
|
+
if len(raw_list) < 5:
|
|
111
|
+
raw_list.append(raw.strip())
|
|
112
|
+
|
|
113
|
+
findings: list[Finding] = []
|
|
114
|
+
for (scontext, tcontext, tclass), info in sorted(grouped.items()):
|
|
115
|
+
perms: set[str] = info["perms"] # type: ignore[assignment]
|
|
116
|
+
comms: set[str] = info["comms"] # type: ignore[assignment]
|
|
117
|
+
raw_list: list[str] = info["lines"] # type: ignore[assignment]
|
|
118
|
+
is_permissive: bool = bool(info["permissive"])
|
|
119
|
+
|
|
120
|
+
rule, note = synthesize_te_rule(scontext, tcontext, tclass, perms)
|
|
121
|
+
neverallow_warn = check_neverallow_risk(scontext, tcontext, tclass, perms)
|
|
122
|
+
|
|
123
|
+
partition = "/vendor" if scontext.startswith(("hal_", "vendor_")) else "/system"
|
|
124
|
+
subdir = "vendor" if partition == "/vendor" else "private"
|
|
125
|
+
target_file = f"device/oem/common/sepolicy/{subdir}/{scontext}.te"
|
|
126
|
+
|
|
127
|
+
comm_desc = f" ({', '.join(sorted(comms))})" if comms else ""
|
|
128
|
+
mode_desc = "permissive" if is_permissive else "enforcing"
|
|
129
|
+
summary = (
|
|
130
|
+
f"Domain '{scontext}'{comm_desc} was denied {{ {' '.join(sorted(perms))} }} "
|
|
131
|
+
f"on '{tcontext}' (class {tclass}) under SELinux {mode_desc} mode."
|
|
132
|
+
)
|
|
133
|
+
if neverallow_warn:
|
|
134
|
+
note = f"WARNING: {neverallow_warn}"
|
|
135
|
+
|
|
136
|
+
findings.append(
|
|
137
|
+
Finding(
|
|
138
|
+
category="selinux",
|
|
139
|
+
severity="high" if not is_permissive else "medium",
|
|
140
|
+
title=f"SELinux AVC denial: {scontext} -> {tcontext}:{tclass}",
|
|
141
|
+
summary=summary,
|
|
142
|
+
partition=f"partition: {partition}",
|
|
143
|
+
target_file=target_file,
|
|
144
|
+
confidence=0.98 if not neverallow_warn else 0.75,
|
|
145
|
+
raw_lines=tuple(raw_list),
|
|
146
|
+
metadata={
|
|
147
|
+
"scontext": scontext,
|
|
148
|
+
"tcontext": tcontext,
|
|
149
|
+
"tclass": tclass,
|
|
150
|
+
"perms": sorted(perms),
|
|
151
|
+
"rule": rule,
|
|
152
|
+
"neverallow_warning": neverallow_warn,
|
|
153
|
+
},
|
|
154
|
+
security_note=note,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
return findings
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def audit_sepolicy_dir(sepolicy_dir: Path) -> list[Finding]:
|
|
161
|
+
"""Scan a directory of .te files for insecure permissive domain declarations."""
|
|
162
|
+
findings: list[Finding] = []
|
|
163
|
+
if not sepolicy_dir.exists():
|
|
164
|
+
return findings
|
|
165
|
+
|
|
166
|
+
for te_file in sorted(sepolicy_dir.rglob("*.te")):
|
|
167
|
+
try:
|
|
168
|
+
content = te_file.read_text(encoding="utf-8", errors="ignore")
|
|
169
|
+
except OSError:
|
|
170
|
+
continue
|
|
171
|
+
for match in PERMISSIVE_TE_RE.finditer(content):
|
|
172
|
+
domain = match.group(1)
|
|
173
|
+
rel = str(te_file)
|
|
174
|
+
findings.append(
|
|
175
|
+
Finding(
|
|
176
|
+
category="selinux",
|
|
177
|
+
severity="high",
|
|
178
|
+
title=f"Permissive SELinux domain detected: {domain}",
|
|
179
|
+
summary=(
|
|
180
|
+
f"'{te_file.name}' declares 'permissive {domain};'. "
|
|
181
|
+
"Permissive domains fail Android CTS CtsSecurityHostTestCases in user builds."
|
|
182
|
+
),
|
|
183
|
+
partition="partition: /vendor",
|
|
184
|
+
target_file=rel,
|
|
185
|
+
confidence=1.0,
|
|
186
|
+
raw_lines=(match.group(0).strip(),),
|
|
187
|
+
metadata={
|
|
188
|
+
"scontext": domain,
|
|
189
|
+
"permissive_line": match.group(0).strip(),
|
|
190
|
+
"remove_permissive": True,
|
|
191
|
+
},
|
|
192
|
+
security_note="Remove permissive declaration and replace with scoped allow rules.",
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return findings
|
acrionix/parsers/tree.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DeviceTreeIndex:
|
|
7
|
+
"""Indexes an AOSP device/vendor tree so patches target real existing files."""
|
|
8
|
+
|
|
9
|
+
def __init__(self, root: Path | None) -> None:
|
|
10
|
+
self.root = root.resolve() if root else None
|
|
11
|
+
self.te_files: dict[str, Path] = {}
|
|
12
|
+
self.manifest_files: list[Path] = []
|
|
13
|
+
self.device_mk_files: list[Path] = []
|
|
14
|
+
self.init_rc_files: list[Path] = []
|
|
15
|
+
self.board_config_files: list[Path] = []
|
|
16
|
+
|
|
17
|
+
if self.root and self.root.exists():
|
|
18
|
+
self._scan()
|
|
19
|
+
|
|
20
|
+
def _scan(self) -> None:
|
|
21
|
+
assert self.root is not None
|
|
22
|
+
for path in sorted(self.root.rglob("*")):
|
|
23
|
+
if not path.is_file():
|
|
24
|
+
continue
|
|
25
|
+
parts = path.parts
|
|
26
|
+
if ".git" in parts or "out" in parts:
|
|
27
|
+
continue
|
|
28
|
+
name = path.name
|
|
29
|
+
if name.endswith(".te"):
|
|
30
|
+
self.te_files[path.stem] = path
|
|
31
|
+
elif name == "manifest.xml" or (name.startswith("manifest_") and name.endswith(".xml")):
|
|
32
|
+
self.manifest_files.append(path)
|
|
33
|
+
elif name == "device.mk" or name.endswith("_device.mk"):
|
|
34
|
+
self.device_mk_files.append(path)
|
|
35
|
+
elif name.endswith(".rc") and "init" in name:
|
|
36
|
+
self.init_rc_files.append(path)
|
|
37
|
+
elif name == "BoardConfig.mk" or name == "BoardConfigCommon.mk":
|
|
38
|
+
self.board_config_files.append(path)
|
|
39
|
+
|
|
40
|
+
def relpath(self, path: Path) -> str:
|
|
41
|
+
if not self.root:
|
|
42
|
+
return str(path)
|
|
43
|
+
try:
|
|
44
|
+
return path.resolve().relative_to(self.root).as_posix()
|
|
45
|
+
except ValueError:
|
|
46
|
+
return path.as_posix()
|
|
47
|
+
|
|
48
|
+
def find_te_file(self, domain: str) -> Path | None:
|
|
49
|
+
return self.te_files.get(domain)
|
|
50
|
+
|
|
51
|
+
def find_manifest(self) -> Path | None:
|
|
52
|
+
return self.manifest_files[0] if self.manifest_files else None
|
|
53
|
+
|
|
54
|
+
def find_device_mk(self) -> Path | None:
|
|
55
|
+
return self.device_mk_files[0] if self.device_mk_files else None
|
|
56
|
+
|
|
57
|
+
def find_init_rc(self, service_name: str | None = None) -> Path | None:
|
|
58
|
+
if service_name:
|
|
59
|
+
for rc in self.init_rc_files:
|
|
60
|
+
try:
|
|
61
|
+
if f"service {service_name}" in rc.read_text(encoding="utf-8", errors="ignore"):
|
|
62
|
+
return rc
|
|
63
|
+
except OSError:
|
|
64
|
+
continue
|
|
65
|
+
return self.init_rc_files[0] if self.init_rc_files else None
|
|
66
|
+
|
|
67
|
+
def summary_counts(self) -> dict[str, int]:
|
|
68
|
+
return {
|
|
69
|
+
"te_files": len(self.te_files),
|
|
70
|
+
"manifests": len(self.manifest_files),
|
|
71
|
+
"device_mk": len(self.device_mk_files),
|
|
72
|
+
"init_rc": len(self.init_rc_files),
|
|
73
|
+
"board_config": len(self.board_config_files),
|
|
74
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Iterable
|
|
5
|
+
|
|
6
|
+
from acrionix.models import Finding
|
|
7
|
+
|
|
8
|
+
HIDL_MISSING_RE = re.compile(
|
|
9
|
+
r"Cannot find entry\s+(?P<pkg>[a-zA-Z0-9_.]+)@(?P<ver>\d+\.\d+)::(?P<iface>[A-Za-z0-9_]+)/(?P<inst>[A-Za-z0-9_.-]+)"
|
|
10
|
+
)
|
|
11
|
+
AIDL_MISSING_RE = re.compile(
|
|
12
|
+
r"(?:Could not find|Cannot find|Failed to get)\s+(?:AIDL\s+)?(?:service|entry)\s+"
|
|
13
|
+
r"(?P<fqname>android\.hardware\.[a-zA-Z0-9_.]+)/(?P<inst>[A-Za-z0-9_.-]+)"
|
|
14
|
+
)
|
|
15
|
+
VINTF_VER_MISMATCH_RE = re.compile(
|
|
16
|
+
r"VintfObject:.*?manifest version\s+(?P<cur>\d+\.\d+)\s+does not satisfy.*?HAL\s+(?P<req>\d+\.\d+)",
|
|
17
|
+
re.IGNORECASE,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def parse_vintf_mismatches(lines: Iterable[str]) -> list[Finding]:
|
|
22
|
+
raw_lines = list(lines)
|
|
23
|
+
findings: list[Finding] = []
|
|
24
|
+
seen: set[str] = set()
|
|
25
|
+
|
|
26
|
+
cur_version_hint: str | None = None
|
|
27
|
+
for line in raw_lines:
|
|
28
|
+
vm = VINTF_VER_MISMATCH_RE.search(line)
|
|
29
|
+
if vm:
|
|
30
|
+
cur_version_hint = vm.group("cur")
|
|
31
|
+
|
|
32
|
+
for line in raw_lines:
|
|
33
|
+
hm = HIDL_MISSING_RE.search(line)
|
|
34
|
+
if hm:
|
|
35
|
+
pkg = hm.group("pkg")
|
|
36
|
+
ver = hm.group("ver")
|
|
37
|
+
iface = hm.group("iface")
|
|
38
|
+
inst = hm.group("inst")
|
|
39
|
+
key = f"hidl:{pkg}@{ver}::{iface}/{inst}"
|
|
40
|
+
if key in seen:
|
|
41
|
+
continue
|
|
42
|
+
seen.add(key)
|
|
43
|
+
|
|
44
|
+
findings.append(
|
|
45
|
+
Finding(
|
|
46
|
+
category="vintf",
|
|
47
|
+
severity="critical",
|
|
48
|
+
title=f"VINTF HIDL mismatch: {pkg}@{ver}::{iface}/{inst}",
|
|
49
|
+
summary=(
|
|
50
|
+
f"hwservicemanager could not resolve {pkg}@{ver}::{iface}/{inst} "
|
|
51
|
+
"in framework or device VINTF manifest. Updating manifest.xml declares "
|
|
52
|
+
"the required HIDL transport and instance."
|
|
53
|
+
),
|
|
54
|
+
partition="partition: /vendor/etc/vintf",
|
|
55
|
+
target_file="device/oem/common/manifest.xml",
|
|
56
|
+
confidence=0.99,
|
|
57
|
+
raw_lines=(line.strip(),),
|
|
58
|
+
metadata={
|
|
59
|
+
"hal_format": "hidl",
|
|
60
|
+
"package": pkg,
|
|
61
|
+
"version": ver,
|
|
62
|
+
"old_version": cur_version_hint,
|
|
63
|
+
"interface": iface,
|
|
64
|
+
"instance": inst,
|
|
65
|
+
},
|
|
66
|
+
security_note="Validated against VINTF manifest schema (checkvintf compatible).",
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
continue
|
|
70
|
+
|
|
71
|
+
am = AIDL_MISSING_RE.search(line)
|
|
72
|
+
if am:
|
|
73
|
+
fqname = am.group("fqname")
|
|
74
|
+
inst = am.group("inst")
|
|
75
|
+
parts = fqname.rsplit(".", 1)
|
|
76
|
+
pkg = parts[0] if len(parts) == 2 else fqname
|
|
77
|
+
iface = parts[1] if len(parts) == 2 else "IService"
|
|
78
|
+
key = f"aidl:{fqname}/{inst}"
|
|
79
|
+
if key in seen:
|
|
80
|
+
continue
|
|
81
|
+
seen.add(key)
|
|
82
|
+
|
|
83
|
+
findings.append(
|
|
84
|
+
Finding(
|
|
85
|
+
category="vintf",
|
|
86
|
+
severity="critical",
|
|
87
|
+
title=f"VINTF AIDL HAL missing: {fqname}/{inst}",
|
|
88
|
+
summary=(
|
|
89
|
+
f"ServiceManager failed to resolve AIDL HAL {fqname}/{inst}. "
|
|
90
|
+
"Declare the AIDL HAL entry in the vendor VINTF manifest fragment."
|
|
91
|
+
),
|
|
92
|
+
partition="partition: /vendor/etc/vintf",
|
|
93
|
+
target_file="device/oem/common/manifest.xml",
|
|
94
|
+
confidence=0.95,
|
|
95
|
+
raw_lines=(line.strip(),),
|
|
96
|
+
metadata={
|
|
97
|
+
"hal_format": "aidl",
|
|
98
|
+
"package": pkg,
|
|
99
|
+
"version": "1",
|
|
100
|
+
"interface": iface,
|
|
101
|
+
"instance": inst,
|
|
102
|
+
},
|
|
103
|
+
security_note="Adds AIDL VINTF manifest stanza with binder transport.",
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return findings
|