diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java new file mode 100644 index 00000000..1f3b633e --- /dev/null +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java @@ -0,0 +1,67 @@ +package com.zalexdev.stryker.hid.configfs; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +/** + * Outcome of {@link UsbGadgetController#applyProfile}. Carries not just success + * but which of the profile's requested functions the kernel could actually + * create, so the UI can warn when (for example) the ACM serial driver is not + * compiled into the kernel and the gadget falls back to HID-only. + */ +public final class ApplyResult { + + public final boolean success; + @Nullable + public final String boundUdc; + /** Functions the profile asked for. */ + public final Set requested; + /** Functions actually created and linked into the gadget. */ + public final Set active; + /** Requested functions the kernel could not provide (requested minus active). */ + public final Set dropped; + + private ApplyResult(boolean success, + @Nullable String boundUdc, + @NonNull Set requested, + @NonNull Set active) { + this.success = success; + this.boundUdc = boundUdc; + this.requested = immutable(requested); + this.active = immutable(active); + EnumSet drop = isEmpty(requested) + ? EnumSet.noneOf(GadgetFunction.class) + : EnumSet.copyOf(requested); + drop.removeAll(active); + this.dropped = Collections.unmodifiableSet(drop); + } + + public boolean hasDropped() { + return !dropped.isEmpty(); + } + + static ApplyResult failure(@NonNull Set requested, + @NonNull Set active) { + return new ApplyResult(false, null, requested, active); + } + + static ApplyResult bound(@NonNull String udc, + @NonNull Set requested, + @NonNull Set active) { + return new ApplyResult(true, udc, requested, active); + } + + private static boolean isEmpty(Set s) { + return s == null || s.isEmpty(); + } + + private static Set immutable(Set s) { + return isEmpty(s) + ? Collections.unmodifiableSet(EnumSet.noneOf(GadgetFunction.class)) + : Collections.unmodifiableSet(EnumSet.copyOf(s)); + } +} diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java index f2ed3737..993b6498 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java @@ -11,12 +11,15 @@ public final class GadgetState { public final String boundUdc; public final Set linkedFunctions; public final String massStorageFile; + /** Whether /dev/hidg0 exists — the same signal the HID Attacks screen uses. */ + public final boolean hidNodePresent; public GadgetState(boolean strykerGadgetExists, boolean strykerGadgetBound, String boundUdc, Set linkedFunctions, - String massStorageFile) { + String massStorageFile, + boolean hidNodePresent) { this.strykerGadgetExists = strykerGadgetExists; this.strykerGadgetBound = strykerGadgetBound; this.boundUdc = boundUdc; @@ -24,5 +27,12 @@ public GadgetState(boolean strykerGadgetExists, ? Collections.unmodifiableSet(EnumSet.noneOf(GadgetFunction.class)) : Collections.unmodifiableSet(EnumSet.copyOf(linkedFunctions)); this.massStorageFile = massStorageFile; + this.hidNodePresent = hidNodePresent; + } + + /** True when this state exposes a HID function (keyboard or mouse). */ + public boolean exposesHid() { + return linkedFunctions.contains(GadgetFunction.HID_KEYBOARD) + || linkedFunctions.contains(GadgetFunction.HID_MOUSE); } } diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java index a9f91dd2..c3e62015 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java @@ -20,6 +20,28 @@ public final class UsbGadgetController { public static final String GADGET_BASE = "/config/usb_gadget/" + GADGET_NAME; private static final String VENDOR_GADGET = "/config/usb_gadget/g1"; + // Android init USB property triggers. Standard across AOSP regardless of the + // SoC-specific init.*.usb.*.rc file that actually carries them. Setting both + // to 0 stops init from re-binding the vendor gadget to the controller right + // after adbd restarts (see Bug 2 in the USB Arsenal report). + private static final String PROP_SYS_CONFIGFS = "sys.usb.configfs"; + private static final String PROP_VENDOR_CONFIGFS = "vendor.usb.configfs"; + private static final String PROP_ADB_TCP_PORT = "service.adb.tcp.port"; + private static final String ADB_TCP_PORT = "5555"; + + // Opt-in: keep ADB reachable over Wi-Fi while our gadget owns the single UDC. + // There is only one UDC, so USB-ADB (the vendor gadget) cannot coexist with + // the Stryker gadget; this is off by default to avoid silently opening an + // unauthenticated network ADB port. + public static final String PREF_KEEP_ADB_TCP = "arsenal_keep_adb_tcp"; + private static final String PREF_SAVED_SYS_CONFIGFS = "arsenal_saved_sys_configfs"; + private static final String PREF_SAVED_VENDOR_CONFIGFS = "arsenal_saved_vendor_configfs"; + // The adbd TCP port as it was before we switched (often empty or -1), and a + // flag marking that we are currently switched, so a second Apply cannot + // overwrite the captured originals with our own 5555 / frozen values. + private static final String PREF_SAVED_ADB_TCP_PORT = "arsenal_saved_adb_tcp_port"; + private static final String PREF_SWITCHED = "arsenal_gadget_switched"; + private static final String KEYBOARD_DESC_HEX = "05010906a101050719e029e71500250175019508810295017508810195057501050819012905910295017503910195067508150025650507190029658100c0"; @@ -54,12 +76,26 @@ public GadgetState readState() { String msFile = exists && linked.contains(GadgetFunction.MASS_STORAGE) ? read(GADGET_BASE + "/functions/mass_storage.0/lun.0/file").trim() : null; - return new GadgetState(exists, bound, bound ? udc : null, linked, msFile); + boolean hidNode = (linked.contains(GadgetFunction.HID_KEYBOARD) + || linked.contains(GadgetFunction.HID_MOUSE)) + && pathExists("/dev/hidg0"); + return new GadgetState(exists, bound, bound ? udc : null, linked, msFile, hidNode); } - public boolean prepare(@NonNull GadgetProfile profile) { - if (!core.checkRoot()) return false; + /** + * Builds the gadget and its functions. Returns the set of functions that were + * actually created and linked into the configuration. A function whose kernel + * driver is missing (e.g. {@code usb_f_acm} on kernels built without ACM) is + * silently skipped rather than aborting the whole gadget, so a HID+ACM profile + * still comes up as HID-only. An empty result means preparation failed (no + * root, or none of the requested functions could be created). + */ + @NonNull + public EnumSet prepare(@NonNull GadgetProfile profile) { + EnumSet created = EnumSet.noneOf(GadgetFunction.class); + if (!core.checkRoot()) return created; List script = new ArrayList<>(); + // Structural setup must fully succeed, so keep set -e for this part. script.add("set -e"); script.add("mkdir -p " + sq(GADGET_BASE)); script.add("mkdir -p " + sq(GADGET_BASE + "/configs/c.1")); @@ -75,6 +111,10 @@ public boolean prepare(@NonNull GadgetProfile profile) { + sq(GADGET_BASE + "/configs/c.1/strings/0x409/configuration")); script.add("echo 120 > " + sq(GADGET_BASE + "/configs/c.1/MaxPower")); + // Function directories can only be created for drivers the kernel has. + // Creating a missing one fails with ENOENT, so drop set -e from here on and + // let each unsupported function be skipped individually. + script.add("set +e"); if (profile.functions.contains(GadgetFunction.HID_KEYBOARD)) { script.addAll(prepareHidKeyboard()); } @@ -96,41 +136,142 @@ public boolean prepare(@NonNull GadgetProfile profile) { script.addAll(prepareEcm()); } + // Link only the functions whose directory actually came up. for (GadgetFunction f : profile.functions) { + String fnDir = GADGET_BASE + "/functions/" + f.functionDir; String link = GADGET_BASE + "/configs/c.1/" + f.functionDir; - script.add("[ -e " + sq(link) + " ] || ln -s " - + sq(GADGET_BASE + "/functions/" + f.functionDir) + " " + sq(link)); + script.add("if [ -d " + sq(fnDir) + " ] && [ ! -e " + sq(link) + " ]; then " + + "ln -s " + sq(fnDir) + " " + sq(link) + " || true; fi"); } - return run(script); + run(script); + + // The kernel is the source of truth: a function counts as created only + // when its link exists in the configuration. + for (GadgetFunction f : profile.functions) { + if (pathExists(GADGET_BASE + "/configs/c.1/" + f.functionDir)) { + created.add(f); + } + } + return created; } @Nullable public String bind() { String udc = capabilities().udcName; if (udc == null) return null; + + // Remember the current trigger-property values so "Restore vendor" can + // put them back exactly as they were. + rememberOriginalTriggerState(); + List script = new ArrayList<>(); + // 1. Freeze Android's USB property triggers BEFORE touching adbd, so init + // cannot re-bind the vendor gadget to the controller once adbd comes + // back and sets sys.usb.ffs.ready=1 (Bug 2). vendor.usb.configfs must + // go first, otherwise the init counter-trigger resets sys.usb.configfs. + script.add("setprop " + PROP_VENDOR_CONFIGFS + " 0"); + script.add("setprop " + PROP_SYS_CONFIGFS + " 0"); + // 2. Stop adbd so it releases the ffs.adb file descriptor that pins the + // vendor gadget; without this, unbinding g1 fails with EBUSY (Bug 1). + script.add("stop adbd 2>/dev/null || true"); + script.add("sleep 1"); + // 3. Unbind the vendor gadget (now succeeds) then bind ours to the UDC. script.add("if [ -s " + sq(VENDOR_GADGET + "/UDC") + " ]; then " + "echo > " + sq(VENDOR_GADGET + "/UDC") + " || true; fi"); - script.add("echo " + sq(udc) + " > " + sq(GADGET_BASE + "/UDC")); - script.add("sleep 1"); - if (run(script)) { + script.add("sleep 0.3"); + script.add("echo " + sq(udc) + " > " + sq(GADGET_BASE + "/UDC") + " || true"); + script.add("sleep 0.5"); + // 4. Optionally keep ADB reachable over Wi-Fi (opt-in, default off). The + // single UDC means USB-ADB cannot coexist with our gadget. + if (core.getBoolean(PREF_KEEP_ADB_TCP)) { + script.add("setprop " + PROP_ADB_TCP_PORT + " " + ADB_TCP_PORT); + script.add("start adbd 2>/dev/null || true"); + } + run(script); + + // Authoritative success check: the vendor-gadget unbind and our bind are + // best-effort (|| true) so the shell never aborts mid-switch, which means + // the shell's exit status says nothing. Read back the UDC and only report + // success when our gadget is actually attached to the controller. + String actual = read(GADGET_BASE + "/UDC").trim(); + if (!actual.isEmpty()) { chmodHidNodes(); - return udc; + return actual; } return null; } public boolean unbind() { List script = new ArrayList<>(); + // Detach our gadget (no ffs fd pins it, so this always succeeds). script.add("if [ -d " + sq(GADGET_BASE) + " ]; then " + "echo > " + sq(GADGET_BASE + "/UDC") + " || true; fi"); + script.add("sleep 0.3"); + // Thaw the init triggers we froze during bind so Android can manage USB + // again, restoring their original values (default 1 when never captured). + script.add("setprop " + PROP_SYS_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_SYS_CONFIGFS))); + script.add("setprop " + PROP_VENDOR_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_VENDOR_CONFIGFS))); + boolean switched = core.getBoolean(PREF_SWITCHED); + if (switched) { + // Put adbd's TCP port back exactly as it was. Leaving 5555 behind keeps + // adbd listening on the network, which is what keeps Android's "USB + // debugging connected" notification on after the cable is unplugged. + script.add("setprop " + PROP_ADB_TCP_PORT + " " + sq(core.getString(PREF_SAVED_ADB_TCP_PORT))); + // adbd only re-reads the port on start, and a plain start is a no-op + // when it is already running in TCP mode, so restart it. + script.add("stop adbd 2>/dev/null || true"); + script.add("sleep 0.5"); + } + script.add("start adbd 2>/dev/null || true"); + // adbd must be up (ffs ready) before the vendor gadget can bind. init's + // property trigger normally rebinds it; wait briefly and, if it did not, + // bind it ourselves for devices whose triggers do not fire. + script.add("sleep 1"); String udc = capabilities().udcName; if (udc != null) { - script.add("if [ -d " + sq(VENDOR_GADGET) + " ]; then " + script.add("if [ -s " + sq(VENDOR_GADGET + "/UDC") + " ]; then : ; " + + "elif [ -d " + sq(VENDOR_GADGET) + " ]; then " + "echo " + sq(udc) + " > " + sq(VENDOR_GADGET + "/UDC") + " || true; fi"); } - return run(script); + boolean ok = run(script); + if (switched) core.putBoolean(PREF_SWITCHED, false); + return ok; + } + + private void rememberOriginalTriggerState() { + // Already switched by a previous Apply: the live values are ours (frozen + // triggers, possibly 5555), so keep the originals captured the first time. + if (core.getBoolean(PREF_SWITCHED)) return; + rememberTrigger(PREF_SAVED_SYS_CONFIGFS, getprop(PROP_SYS_CONFIGFS)); + rememberTrigger(PREF_SAVED_VENDOR_CONFIGFS, getprop(PROP_VENDOR_CONFIGFS)); + // An empty port is a legitimate original value, so store it as-is. + core.putString(PREF_SAVED_ADB_TCP_PORT, getprop(PROP_ADB_TCP_PORT)); + core.putBoolean(PREF_SWITCHED, true); + } + + private void rememberTrigger(String prefKey, String current) { + // Never persist our own frozen "0" as the original; otherwise a second + // Apply before a Restore would make us "restore" the frozen value. A real + // value (normally "1") is captured; "0"/blank keeps the prior original. + if (current != null && !current.isEmpty() && !current.equals("0")) { + core.putString(prefKey, current); + } + } + + private String savedTriggerValue(String prefKey) { + String v = core.getString(prefKey); + return (v == null || v.isEmpty()) ? "1" : v; + } + + private String getprop(String name) { + List out = core.customCommand("getprop " + sq(name), true); + if (out != null) { + for (String line : out) { + if (line != null && !line.trim().isEmpty()) return line.trim(); + } + } + return ""; } public boolean teardown() { @@ -147,17 +288,24 @@ public boolean teardown() { return run(script); } - public boolean apply(@NonNull GadgetProfile profile) { - if (!prepare(profile)) { + @NonNull + public ApplyResult applyProfile(@NonNull GadgetProfile profile) { + EnumSet created = prepare(profile); + if (created.isEmpty()) { + // Nothing the profile asked for is supported by this kernel. teardown(); - return false; + return ApplyResult.failure(profile.functions, created); } String udc = bind(); if (udc == null) { unbind(); - return false; + return ApplyResult.failure(profile.functions, created); } - return true; + return ApplyResult.bound(udc, profile.functions, created); + } + + public boolean apply(@NonNull GadgetProfile profile) { + return applyProfile(profile).success; } public boolean swapMassStorageImage(String absoluteImagePath, diff --git a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java index 07d9fe03..791e2343 100644 --- a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java +++ b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java @@ -15,10 +15,12 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.switchmaterial.SwitchMaterial; import com.google.android.material.textview.MaterialTextView; import com.zalexdev.stryker.R; import com.zalexdev.stryker.hid.capability.HidCapabilities; import com.zalexdev.stryker.hid.capability.HidCapabilityProbe; +import com.zalexdev.stryker.hid.configfs.ApplyResult; import com.zalexdev.stryker.hid.configfs.GadgetFunction; import com.zalexdev.stryker.hid.configfs.GadgetProfile; import com.zalexdev.stryker.hid.configfs.GadgetState; @@ -28,6 +30,7 @@ import java.util.Iterator; import java.util.Locale; +import java.util.Set; public class UsbArsenalFragment extends Fragment implements GadgetProfileAdapter.Callbacks { @@ -69,6 +72,11 @@ public void onViewCreated(@NonNull View v, @Nullable Bundle savedInstanceState) statePill = v.findViewById(R.id.arsenal_state_pill); stateDetail = v.findViewById(R.id.arsenal_state_detail); + SwitchMaterial keepAdbSwitch = v.findViewById(R.id.arsenal_keep_adb_switch); + keepAdbSwitch.setChecked(core.getBoolean(UsbGadgetController.PREF_KEEP_ADB_TCP)); + keepAdbSwitch.setOnCheckedChangeListener((btn, checked) -> + core.putBoolean(UsbGadgetController.PREF_KEEP_ADB_TCP, checked)); + RecyclerView list = v.findViewById(R.id.arsenal_profile_list); list.setLayoutManager(new LinearLayoutManager(requireContext())); adapter = new GadgetProfileAdapter(this); @@ -127,6 +135,9 @@ private void bindState(@NonNull HidCapabilities caps, @NonNull GadgetState state detail.append("udc=").append(caps.udcName == null ? "—" : caps.udcName) .append(" · configfs=").append(caps.configFsMounted ? "✓" : "✗") .append(" · stryker_gadget=").append(state.strykerGadgetExists ? "✓" : "✗"); + if (state.exposesHid()) { + detail.append(" · hidg0=").append(state.hidNodePresent ? "✓" : "✗"); + } if (!state.linkedFunctions.isEmpty()) { detail.append('\n').append("functions="); Iterator it = state.linkedFunctions.iterator(); @@ -147,14 +158,22 @@ public void onApply(@NonNull GadgetProfile profile) { statePill.setBackgroundResource(R.drawable.chip_pill_warn); statePill.setTextColor(0xFFEF6C00); new Thread(() -> { - boolean teardownOk = controller.teardown(); - boolean ok = controller.apply(profile); + controller.teardown(); + ApplyResult result = controller.applyProfile(profile); GadgetState state = controller.readState(); HidCapabilities caps = new HidCapabilityProbe(requireContext()).probe(); requireActivity().runOnUiThread(() -> { bindState(caps, state); - if (ok && state.boundUdc != null) { - core.toaster(getString(R.string.arsenal_apply_ok, state.boundUdc)); + if (result.success && state.boundUdc != null) { + if (result.hasDropped()) { + core.toaster(getString(R.string.arsenal_apply_partial, + functionLabels(result.dropped))); + } else { + core.toaster(getString(R.string.arsenal_apply_ok, state.boundUdc)); + } + } else if (!result.success && result.active.isEmpty() && result.hasDropped()) { + core.toaster(getString(R.string.arsenal_apply_unsupported, + functionLabels(result.dropped))); } else { core.toaster(getString(R.string.arsenal_apply_failed)); } @@ -162,6 +181,28 @@ public void onApply(@NonNull GadgetProfile profile) { }, "arsenal-apply").start(); } + private String functionLabels(@NonNull Set fns) { + StringBuilder sb = new StringBuilder(); + for (GadgetFunction f : fns) { + if (sb.length() > 0) sb.append(", "); + sb.append(getString(functionLabelRes(f))); + } + return sb.toString(); + } + + private static int functionLabelRes(@NonNull GadgetFunction f) { + switch (f) { + case HID_KEYBOARD: return R.string.arsenal_function_hid_kbd; + case HID_MOUSE: return R.string.arsenal_function_hid_mouse; + case MASS_STORAGE: return R.string.arsenal_function_mass; + case RNDIS: return R.string.arsenal_function_rndis; + case ECM: return R.string.arsenal_function_ecm; + case ACM: return R.string.arsenal_function_acm; + case ADB: return R.string.arsenal_function_adb; + default: return R.string.arsenal_functions_label; + } + } + @Override public void onEdit(@NonNull GadgetProfile profile) { openEditor(profile); diff --git a/app/src/main/res/layout/fragment_usb_arsenal.xml b/app/src/main/res/layout/fragment_usb_arsenal.xml index 1a934b24..9bf03e8a 100644 --- a/app/src/main/res/layout/fragment_usb_arsenal.xml +++ b/app/src/main/res/layout/fragment_usb_arsenal.xml @@ -122,6 +122,23 @@ android:textColor="@color/grey" android:textSize="11sp" /> + + + + Emulate CD-ROM Inquiry string (28 ASCII) Profile applied — gadget bound to %s + Applied without %1$s — this kernel has no driver for it + Can\'t apply: %1$s not supported by this kernel Failed to apply profile (check logcat) Vendor gadget restored + Keep ADB over Wi-Fi (TCP 5555) + Applying a profile takes over USB, so USB-ADB drops. Turn this on to switch ADB to Wi-Fi (adb connect PHONE_IP:5555) first. Off by default — this opens an unauthenticated ADB port on your network. Save Cancel VID must look like 0x046d