Restrict library redaction exceptions to loader diagnostics

This commit is contained in:
wallentx committed 2026-10-18 04:05:36 -05:00
1 parent 4ab4a86507
commit 3e90875be8
2 files changed
+79 -18

No files matched your search

@@ -50,11 +50,31 @@ object LogRedactor {
private fun matchesEntire(input: String, regex: Regex): Boolean =
withMatcher(regex, input) { it.matches() }
private fun isHidlSharedLibrary(input: String, match: java.util.regex.Matcher): Boolean {
private fun isHidlLoaderDiagnostic(input: String, match: java.util.regex.Matcher): Boolean {
if (!matchesEntire(match.group(), HIDL_SHARED_LIBRARY)) return false
fun continuesToken(char: Char) = char.isLetterOrDigit() || char in "._%+@-"
return (match.start() == 0 || !continuesToken(input[match.start() - 1])) &&
(match.end() == input.length || !continuesToken(input[match.end()]))
val start = match.start()
val end = match.end()
val prefix = "library \""
val suffix = "\" not found"
val earliestQuote = (start - 4096).coerceAtLeast(0)
var quote = start - 1
while (quote >= earliestQuote && input[quote] != '"') {
val char = input[quote]
if (!char.isLetterOrDigit() && char !in "/._-") return false
quote--
}
if (quote < earliestQuote) return false
val pathStart = quote + 1
if (pathStart < start && (input[start - 1] != '/' ||
!(input.regionMatches(pathStart, "/vendor/", 0, 8) ||
input.regionMatches(pathStart, "/system/", 0, 8)))) return false
val prefixStart = quote - prefix.length + 1
val suffixEnd = end + suffix.length
return prefixStart >= 0 && suffixEnd <= input.length &&
input.regionMatches(prefixStart, prefix, 0, prefix.length, ignoreCase = true) &&
input.regionMatches(end, suffix, 0, suffix.length, ignoreCase = true) &&
(prefixStart == 0 || !(input[prefixStart - 1].isLetterOrDigit() || input[prefixStart - 1] == '_')) &&
(suffixEnd == input.length || !(input[suffixEnd].isLetterOrDigit() || input[suffixEnd] == '_'))
}
private val EMAIL = Regex("[A-Za-z0-9._%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,}")
@@ -66,10 +86,10 @@ object LogRedactor {
private val JWT_BASE64 = Regex("ey[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}")
private val GUID = Regex("\\b[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\\b")
private val SECRET_KV = Regex(
"(?i)\\b(access[_-]?token|refresh[_-]?token|auth[_-]?token|authtoken|token|authcode|" +
"(?i)(\\b(?:access[_-]?token|refresh[_-]?token|auth[_-]?token|authtoken|token|authcode|" +
"auth[_-]?ticket|ticket|sessionid|steamloginsecure|webapikey|api[_-]?key|" +
"machine[_-]?auth(?:[_-]?token)?|machineauth|password|passwd|pwd|secret)" +
"(\\s*[=:]\\s*|=)([^\\s\"'<>&;,]{4,})"
"machine[_-]?auth(?:[_-]?token)?|machineauth|password|passwd|pwd|secret)[\"']?\\s*[=:]\\s*)" +
"(?:([\"'])((?:\\\\.|(?!\\2)[^\\\\\\r\\n]){4,})\\2|([^\\s\"'<>&;,]{4,}))"
)
private val GUARD_CODE = Regex(
"(?i)((?:steam\\s*)?guard\\s*code[\\s:=]*|two[\\s-]?factor[\\s:=]*|2fa[\\s:=]*)([A-Za-z0-9]{5})"
@@ -356,7 +376,10 @@ object LogRedactor {
if (out.has("jwt")) out.set(replaceMatches(out.text, JWT_LABELLED) { "${it.group(1)}<redacted:jwt>" })
if ("ey" in out.text) out.set(replaceMatches(out.text, JWT_BASE64, "<redacted:jwt>"))
if (out.has("token", "auth", "ticket", "sessionid", "steamloginsecure", "api", "machine", "pass", "pwd", "secret")) {
out.set(replaceMatches(out.text, SECRET_KV) { "${it.group(1)}${it.group(2)}<redacted:token>" })
out.set(replaceMatches(out.text, SECRET_KV) {
val quote = it.group(2).orEmpty()
"${it.group(1)}${quote}<redacted:token>${quote}"
})
}
if (out.has("guard", "factor", "2fa")) out.set(replaceMatches(out.text, GUARD_CODE) { "${it.group(1)}<redacted:code>" })
if (out.hasRun(32, ::isHex)) out.set(replaceMatches(out.text, WEBAPI_KEY, "<redacted:key>"))
@@ -375,7 +398,7 @@ object LogRedactor {
if (out.has("login")) out.set(replaceMatches(out.text, LOGIN_KV) { "${it.group(1)}${it.group(2)}${quoted(it.group(3))}" })
for (r in accounts) if (r.needs == null || out.has(r.needs)) out.set(replaceMatches(out.text, r.regex, "<redacted:account>"))
if ('@' in out.text) out.set(replaceMatches(out.text, EMAIL) { m ->
m.group().takeIf { isHidlSharedLibrary(out.text, m) } ?: "<redacted:email>"
m.group().takeIf { isHidlLoaderDiagnostic(out.text, m) } ?: "<redacted:email>"
})
if (out.has("jwt", "token", "ticket", "sessionid", "steamloginsecure", "machineauth")) {
out.set(replaceMatches(out.text, RESIDUAL) { "${it.group(1)}=<redacted:token>" })
@@ -42,35 +42,54 @@ class LogRedactorTest {
assertEquals("login <redacted:email> ok", LogRedactor.redact("login someone@example.com ok"))
}
@Test fun hidlSharedLibrariesStayVisibleWhileAdjacentSecretsAndEmailsAreRedacted() {
@Test fun onlyLoaderDiagnosticsKeepHidlSharedLibraryNames() {
val library = "android.hardware.graphics.mapper@4.0.so"
val vendorLibrary = "vendor.qti.hardware.display.mapper@4.0.so"
val line = "/vendor/lib64/hw/$library loaded $vendorLibrary contact=someone@company.so token=abcdef123456"
val expected = "/vendor/lib64/hw/$library loaded $vendorLibrary contact=<redacted:email> token=<redacted:token>"
val line = "dlopen failed: library \"$library\" not found"
val pathLine = "dlopen failed: library \"/vendor/lib64/hw/$library\" not found"
assertEquals(expected, LogRedactor.redact(line))
assertEquals(expected, LogRedactor.redactForShare(line))
assertEquals(line, LogRedactor.redact(line))
assertEquals(line, LogRedactor.redactForShare(line))
assertEquals(pathLine, LogRedactor.redact(pathLine))
assertEquals(pathLine, LogRedactor.redactForShare(pathLine))
}
@Test fun privacyRulesOverrideTheHidlSharedLibraryException() {
val library = "android.hardware.graphics.mapper@4.0.so"
assertEquals("token=<redacted:token>", LogRedactor.redact("token=$library"))
assertEquals("username=<redacted:account>", LogRedactor.redact("username=$library"))
assertEquals("{\"password\":\"<redacted:token>\"}", LogRedactor.redact("{\"password\":\"$library\"}"))
assertEquals("{\"password\":\"<redacted:token>\"}", LogRedactor.redactForShare("{\"password\":\"$library\"}"))
assertEquals("{\"password\":\"<redacted:token>\"}", LogRedactor.redact("{\"password\":\"don't-use-$library\"}"))
assertEquals("{\"password\":\"<redacted:token>\"}", LogRedactor.redactForShare("{\"password\":\"don't-use-$library\"}"))
val users = tmp.newFile("hidl-loginusers.vdf")
val mixed = "dlopen failed: library \"$library\" not found; token=abcdef123456; contact=alice!$library; {\"password\":\"library \\\"$library\\\" not found\"}"
val expected = "dlopen failed: library \"$library\" not found; token=<redacted:token>; contact=alice!<redacted:email>; {\"password\":\"<redacted:token>\"}"
assertEquals(expected, LogRedactor.redact(mixed))
assertEquals(expected, LogRedactor.redactForShare(mixed))
val root = java.nio.file.Files.createTempDirectory("hidl-redactor").toFile()
val users = java.io.File(root, "loginusers.vdf")
users.writeText("\"AccountName\" \"$library\"")
try {
LogRedactor.learnAccounts(users)
assertEquals("<redacted:account>", LogRedactor.redact(library))
} finally {
LogRedactor.learnAccounts()
root.deleteRecursively()
}
}
@Test fun onlyExactHidlSharedLibraryNamesBypassEmailRedaction() {
@Test fun hidlSharedLibraryNamesAreRedactedOutsideLoaderDiagnostics() {
val library = "android.hardware.graphics.mapper@4.0.so"
assertEquals(library, LogRedactor.redact(library))
assertEquals("<redacted:email>", LogRedactor.redact(library))
assertEquals("<redacted:email>", LogRedactor.redactForShare(library))
assertEquals("/vendor/lib64/hw/<redacted:email>", LogRedactor.redact("/vendor/lib64/hw/$library"))
for (punctuation in listOf("!", "#", "~", "/", "=")) {
val expected = "contact=alice$punctuation<redacted:email>"
assertEquals(expected, LogRedactor.redact("contact=alice$punctuation$library"))
assertEquals(expected, LogRedactor.redactForShare("contact=alice$punctuation$library"))
}
assertEquals("<redacted:email>", LogRedactor.redact("$library.evil"))
assertEquals("<redacted:email>", LogRedactor.redact("someone@company.so"))
assertEquals("<redacted:email>", LogRedactor.redact("not$library"))
@@ -81,6 +100,25 @@ class LogRedactorTest {
assertEquals("<redacted:email>", LogRedactor.redact("android.hardware.graphics.mapper@4.0.so.evil"))
}
@Test fun loaderContextRequiresExactWordsAndABoundedAndroidPath() {
val library = "android.hardware.graphics.mapper@4.0.so"
val valid = "library \"/system/lib64/$library\" not found: needed by app"
assertEquals(valid, LogRedactor.redact(valid))
assertEquals(valid, LogRedactor.redactForShare(valid))
for (line in listOf(
"not_library \"$library\" not found",
"library \"$library\" not found_suffix",
"library \"/home/user/$library\" not found",
"library \"relative/$library\" not found",
"library \"/vendor/invalid!/$library\" not found",
"library \"/vendor/" + "a/".repeat(2050) + "$library\" not found",
)) {
val expected = line.replace(library, "<redacted:email>")
assertEquals(expected, LogRedactor.redact(line))
assertEquals(expected, LogRedactor.redactForShare(line))
}
}
@Test fun clockTimeIsNotAnAddress() {
val line = "[2026-09-27 10:03:05] Startup"
assertEquals(line, LogRedactor.redact(line))