Modern Apps git

← Modern-Apps.git

logviewer: bound report decompression so a crafted extra cannot OOM

Commit
cfc8ee4659a1cdedd30ca13ce5887b9dff89a869
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:05 UTC (29 days ago)
Parents
d92e852
Symptom: a malformed ACTION_ERROR_REPORT intent could kill the
viewer process. readReportMessageBytes gunzipped the gzipped_msg
extra with an unbounded readBytes(); the extra is binder-sized
(~1 MiB) but gzip expands ~1000x, so a few hundred KiB of zeros
becomes a gigabyte allocation and an OutOfMemoryError. The
text-tombstone path had the same shape via Files.readAllBytes on
a caller-named (mtime-checked) file.

Root cause: both report-body reads trusted their input size.
The senders are gated (signature|preinstalled), but "preinstalled"
is a wide set to hand an unbounded-allocation primitive.

Fix: read both through readBounded(), which aborts past 16 MiB -
far above any real stack trace or text tombstone. Oversized input
now degrades exactly like corrupt input: Unavailable (with the
existing "Unable to show more info" message on the More info
path). The post-read mtime recheck on tombstones is unchanged.

Regression: ErrorReportReaderTest gains a gzip bomb case (20 MiB
of zeros stays unavailable, no OOM) and an oversized-tombstone
case; suite green.

Diff

logviewer/src/main/java/com/vayunmathur/logviewer/platform/ErrorReportReader.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/ErrorReportReader.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/ErrorReportReader.ktindex 8549731736..471f1bf358 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/ErrorReportReader.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/ErrorReportReader.kt@@ -18,9 +18,11 @@ import com.vayunmathur.logviewer.domain.LogDocument import com.vayunmathur.logviewer.domain.LogKind import com.vayunmathur.logviewer.intents.LogViewerIntents import java.io.ByteArrayInputStream+import java.io.ByteArrayOutputStream import java.io.File+import java.io.FileInputStream import java.io.IOException-import java.nio.file.Files+import java.io.InputStream import java.util.zip.GZIPInputStream  /**@@ -41,6 +43,14 @@ internal object ErrorReportReader {      private const val OS_VERSION_PREFIX = "osVersion: " +    /**+     * Largest report body accepted from an intent extra or a tombstone file. Real reports+     * are stack traces (kilobytes to a few megabytes with memory maps); the cap exists so a+     * crafted extra - a binder-sized gzip expands ~1000x - cannot OOM this process while+     * decompressing. Oversized input degrades to Unavailable like corrupt input does.+     */+    private const val MAX_REPORT_BYTES = 16 shl 20+     fun read(context: Context, intent: Intent): LogLoadResult = when (intent.action) {         LogViewerIntents.ACTION_ERROR_REPORT -> readErrorReport(context, intent)         Intent.ACTION_APP_ERROR -> readAppError(context, intent)@@ -107,13 +117,28 @@ internal object ErrorReportReader {         val gzipped = extras.getByteArray(LogViewerIntents.EXTRA_GZIPPED_MESSAGE)             ?: return null         return try {-            GZIPInputStream(ByteArrayInputStream(gzipped)).use { it.readBytes() }+            GZIPInputStream(ByteArrayInputStream(gzipped)).use { readBounded(it) }         } catch (e: IOException) {             Log.dev(TAG, "corrupt gzipped message", e)             null         }     } +    /** Reads [input] fully, or returns null once it passes [MAX_REPORT_BYTES]. */+    private fun readBounded(input: InputStream): ByteArray? {+        val out = ByteArrayOutputStream()+        val buffer = ByteArray(64 * 1024)+        var total = 0+        while (true) {+            val read = input.read(buffer)+            if (read < 0) break+            total += read+            if (total > MAX_REPORT_BYTES) return null+            out.write(buffer, 0, read)+        }+        return out.toByteArray()+    }+     private fun buildReportHeader(         context: Context,         extras: Bundle,@@ -386,11 +411,11 @@ internal object ErrorReportReader {         val timestamped = textTombstoneFile(intent) ?: return null         val file = timestamped.file         val bytes = try {-            Files.readAllBytes(file.toPath())+            FileInputStream(file).use { readBounded(it) }         } catch (e: IOException) {             Log.error(TAG, "unable to read tombstone $file", e)             return null-        }+        } ?: return null         // Rechecked after the read: the ring may have recycled the file mid-read, and half of one         // crash followed by half of another is worse than nothing.         if (file.lastModified() != timestamped.lastModified) return null
logviewer/src/test/java/com/vayunmathur/logviewer/platform/ErrorReportReaderTest.kt
diff --git a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/ErrorReportReaderTest.kt b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/ErrorReportReaderTest.ktindex b1393866e3..d8eaf2cbaf 100644--- a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/ErrorReportReaderTest.kt+++ b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/ErrorReportReaderTest.kt@@ -141,6 +141,16 @@ class ErrorReportReaderTest {         assertEquals("*** *** *** ***", fromTombstone.bodyLines[fromTombstone.bodyLines.size - 2])     } +    @Test+    fun `gzipped message expanding past the size cap is unavailable, not an OOM`() {+        // ~20 MiB of zeros compresses to a few KiB: small on the binder, huge in memory.+        val bomb = ByteArrayOutputStream()+        GZIPOutputStream(bomb).use { it.write(ByteArray(20 shl 20)) }+        val intent = Intent(LogViewerIntents.ACTION_ERROR_REPORT)+            .putExtra(LogViewerIntents.EXTRA_GZIPPED_MESSAGE, bomb.toByteArray())+        assertNull(unavailable(ErrorReportReader.read(context, intent)).messageRes)+    }+     @Test     fun `empty gzipped message still loads with only the header`() {         val doc = loaded(ErrorReportReader.read(context, errorReportIntent("")))@@ -170,6 +180,13 @@ class ErrorReportReaderTest {         assertTrue(doc.bodyLines.contains("full native trace"))     } +    @Test+    fun `preferring an oversized tombstone explains the failure`() {+        val file = tombstoneFile("x".repeat((16 shl 20) + 1))+        val result = unavailable(ErrorReportReader.read(context, tombstoneIntent(file)))+        assertEquals(R.string.toast_unable_to_show_more_info, result.messageRes)+    }+     @Test     fun `preferring a tombstone with a stale timestamp explains the failure`() {         val file = tombstoneFile("contents")