Harden smali hooks against branch targets and register limits #59

Closed
opened 2026-10-11 09:15:15 +02:00 by midas · 0 comments
Owner

Found in the codebase review (2026-10-11). None of these is known to be broken today (the methods look straight-line), but each would fail silently, at runtime, after an app update.

1. Hooks inserted before a return or invoke can be skipped by branches. Plain addInstructions(index, ...) leaves existing labels on the original instruction, so a goto/if-* that targets that return/invoke jumps past the hook. The safe pattern is already used and documented in edition/contentfilters/ContentFilterPatches.kt and edition/preferredsource/HidePreferredSourcePatch.kt: replaceInstruction(index, "<hook invoke>"), then addInstructions(index + 1, "move-result ... / <original instruction>"). Sites:

  • edition/reviewprompts/HideReviewPromptsPatch.kt (the boolean && chain)
  • dpg/videosdk/VideoProductionCachePatch.kt (kotlinx.serialization constructors often have goto -> return-void paths)
  • kubus/markdown/CopyArticleMarkdownPatch.kt (ArticleDetailResponse.<init>, and before MutableStateFlow.setValue)
  • kubus/settings/MidasSettingsPatch.kt (getSections)
  • edition/onboarding/HideOnboardingPopupsPatch.kt (the isSuppressed return)

2. const/4 and 35c/22c forms on parameter registers only take v0-v15. If a method grows locals, patching fails (loudly, at least). Sites: const/4 p1 in edition/ads/HideAdsPatch.kt, kubus/ads/HideAdsPatch.kt, edition/update/DisableUpdatePromptsPatch.kt, videoland/ads/HideAdsPatch.kt (3x); instance-of v0, p1 / invoke-static {p1} in several hooks. Use const/16 and /range invokes where the register isn't guaranteed small (kubus/ads/HideAdsPatch.kt already does).

3. edition/markdown/CopyArticleMarkdownPatch.kt takes the list from registerD of the addShareAction call, which assumes an instance method with the list as first argument. If R8 makes it static, the extension gets the toolbar object instead. Derive the register from the MethodReference parameter types (index of Ljava/util/List;, +1 when not static).

Done when: all sites converted, buildAndroid passes and patch-install.ps1 -NoInstall / check-patches.sh print Applied for every patch on Volkskrant, Trouw, Het Parool, the Kubus titles and Videoland; a quick device smoke test of the touched features.

Found in the codebase review (2026-10-11). None of these is known to be broken today (the methods look straight-line), but each would fail silently, at runtime, after an app update. **1. Hooks inserted before a `return` or invoke can be skipped by branches.** Plain `addInstructions(index, ...)` leaves existing labels on the original instruction, so a `goto`/`if-*` that targets that return/invoke jumps past the hook. The safe pattern is already used and documented in `edition/contentfilters/ContentFilterPatches.kt` and `edition/preferredsource/HidePreferredSourcePatch.kt`: `replaceInstruction(index, "<hook invoke>")`, then `addInstructions(index + 1, "move-result ... / <original instruction>")`. Sites: - `edition/reviewprompts/HideReviewPromptsPatch.kt` (the boolean `&&` chain) - `dpg/videosdk/VideoProductionCachePatch.kt` (kotlinx.serialization constructors often have `goto` -> `return-void` paths) - `kubus/markdown/CopyArticleMarkdownPatch.kt` (`ArticleDetailResponse.<init>`, and before `MutableStateFlow.setValue`) - `kubus/settings/MidasSettingsPatch.kt` (`getSections`) - `edition/onboarding/HideOnboardingPopupsPatch.kt` (the `isSuppressed` return) **2. `const/4` and 35c/22c forms on parameter registers** only take v0-v15. If a method grows locals, patching fails (loudly, at least). Sites: `const/4 p1` in `edition/ads/HideAdsPatch.kt`, `kubus/ads/HideAdsPatch.kt`, `edition/update/DisableUpdatePromptsPatch.kt`, `videoland/ads/HideAdsPatch.kt` (3x); `instance-of v0, p1` / `invoke-static {p1}` in several hooks. Use `const/16` and `/range` invokes where the register isn't guaranteed small (`kubus/ads/HideAdsPatch.kt` already does). **3. `edition/markdown/CopyArticleMarkdownPatch.kt`** takes the list from `registerD` of the `addShareAction` call, which assumes an instance method with the list as first argument. If R8 makes it static, the extension gets the toolbar object instead. Derive the register from the `MethodReference` parameter types (index of `Ljava/util/List;`, +1 when not static). **Done when:** all sites converted, `buildAndroid` passes and `patch-install.ps1 -NoInstall` / `check-patches.sh` print `Applied` for every patch on Volkskrant, Trouw, Het Parool, the Kubus titles and Videoland; a quick device smoke test of the touched features.
midas closed this issue 2026-10-11 12:29:53 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
midas/midas-patches#59
No description provided.