From 1e94179ddf622d53e268fab1d8ae5dc9f44db639 Mon Sep 17 00:00:00 2001 From: tiann Date: Wed, 25 May 2022 10:54:01 +0800 Subject: [PATCH] minor fixes Signed-off-by: tiann --- .../virtual/client/fixer/ContextFixer.java | 21 ++++++++++++++ .../client/hook/providers/ProviderHook.java | 28 +++++++++++-------- .../server/pm/parser/PackageParserEx.java | 14 ++++++++++ .../java/mirror/android/app/ContextImpl.java | 2 ++ 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/VirtualApp/lib/src/main/java/com/lody/virtual/client/fixer/ContextFixer.java b/VirtualApp/lib/src/main/java/com/lody/virtual/client/fixer/ContextFixer.java index 121d0d41a..bc50cbc37 100644 --- a/VirtualApp/lib/src/main/java/com/lody/virtual/client/fixer/ContextFixer.java +++ b/VirtualApp/lib/src/main/java/com/lody/virtual/client/fixer/ContextFixer.java @@ -5,6 +5,7 @@ import android.os.Build; import android.os.DropBoxManager; +import com.lody.virtual.client.VClientImpl; import com.lody.virtual.client.core.InvocationStubManager; import com.lody.virtual.client.core.VirtualCore; import com.lody.virtual.client.hook.base.BinderInvocationStub; @@ -70,6 +71,26 @@ public static void fixContext(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { ContentResolverJBMR2.mPackageName.set(context.getContentResolver(), hostPkg); } + + if (ContextImpl.getAttributionSource != null) { + fixAttributionSource(ContextImpl.getAttributionSource.call(context), hostPkg, VClientImpl.get().getVUid()); + } + } + + public static void fixAttributionSource(Object attr, String pkg, int uid) { + if (attr == null) { + return; + } + try { + Object mAttributionSourceState = Reflect.on(attr).get("mAttributionSourceState"); + Reflect.on(mAttributionSourceState).set("uid", uid); + Reflect.on(mAttributionSourceState).set("packageName", pkg); + + Object next = Reflect.on(attr).call("getNext").get(); + fixAttributionSource(next, pkg, uid); + } catch (Throwable e) { + e.printStackTrace(); + } } } diff --git a/VirtualApp/lib/src/main/java/com/lody/virtual/client/hook/providers/ProviderHook.java b/VirtualApp/lib/src/main/java/com/lody/virtual/client/hook/providers/ProviderHook.java index 77b67428f..ffb032e04 100644 --- a/VirtualApp/lib/src/main/java/com/lody/virtual/client/hook/providers/ProviderHook.java +++ b/VirtualApp/lib/src/main/java/com/lody/virtual/client/hook/providers/ProviderHook.java @@ -10,9 +10,9 @@ import android.os.ParcelFileDescriptor; import com.lody.virtual.client.core.VirtualCore; +import com.lody.virtual.client.fixer.ContextFixer; import com.lody.virtual.client.hook.base.MethodBox; import com.lody.virtual.helper.compat.BuildCompat; -import com.lody.virtual.helper.utils.Reflect; import com.lody.virtual.helper.utils.VLog; import java.lang.reflect.InvocationHandler; @@ -162,6 +162,9 @@ public Object invoke(Object proxy, Method method, Object... args) throws Throwab // Android 11: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/content/IContentProvider.java?q=IContentProvider&ss=android%2Fplatform%2Fsuperproject start = 2; } + if (BuildCompat.isS() ) { + tryFixAttributionSource(args); + } try { String name = method.getName(); if ("call".equals(name)) { @@ -214,9 +217,7 @@ public Object invoke(Object proxy, Method method, Object... args) throws Throwab String[] selectionArgs = null; String sortOrder = null; Bundle queryArgs = null; - if (BuildCompat.isS()) { - fixAttributionSource(args[0]); - } + if (Build.VERSION.SDK_INT >= 26) { queryArgs = (Bundle) args[start + 2]; if (queryArgs != null) { @@ -249,13 +250,18 @@ public interface HookFetcher { ProviderHook fetch(boolean external, IInterface provider); } - private void fixAttributionSource(Object attribution) { - try { - Object mAttributionSourceState = Reflect.on(attribution).get("mAttributionSourceState"); - Reflect.on(mAttributionSourceState).set("uid", VirtualCore.get().myUid()); - Reflect.on(mAttributionSourceState).set("packageName", VirtualCore.get().getHostPkg()); - } catch (Throwable e) { - e.printStackTrace(); + private void tryFixAttributionSource(Object[] args) { + if (args == null || args.length == 0) { + return; + } + Object attribution = args[0]; + if (attribution == null) { + return; } + if (!attribution.getClass().getName().equals("android.content.AttributionSource")) { + return; + } + + ContextFixer.fixAttributionSource(attribution, VirtualCore.get().getHostPkg(), VirtualCore.get().myUid()); } } diff --git a/VirtualApp/lib/src/main/java/com/lody/virtual/server/pm/parser/PackageParserEx.java b/VirtualApp/lib/src/main/java/com/lody/virtual/server/pm/parser/PackageParserEx.java index 47cb837df..e1d69702c 100644 --- a/VirtualApp/lib/src/main/java/com/lody/virtual/server/pm/parser/PackageParserEx.java +++ b/VirtualApp/lib/src/main/java/com/lody/virtual/server/pm/parser/PackageParserEx.java @@ -450,6 +450,15 @@ public static PackageInfo generatePackageInfo(VPackage p, int flags, long firstI return pi; } + private static boolean copyNeeded(int flags, VPackage p, + PackageUserState state, Bundle metaData, int userId) { + if (!state.installed || state.hidden) { + return true; + } + return (flags & PackageManager.GET_META_DATA) != 0 + && (metaData != null || p.mAppMetaData != null); + } + public static ApplicationInfo generateApplicationInfo(VPackage p, int flags, PackageUserState state, int userId) { if (p == null) return null; @@ -457,6 +466,11 @@ public static ApplicationInfo generateApplicationInfo(VPackage p, int flags, return null; } + if (!copyNeeded(flags, p, state, null, userId)) { + initApplicationAsUser(p.applicationInfo, userId); + return p.applicationInfo; + } + // Make shallow copy so we can store the metadata/libraries safely ApplicationInfo ai = new ApplicationInfo(p.applicationInfo); if ((flags & PackageManager.GET_META_DATA) != 0) { diff --git a/VirtualApp/lib/src/main/java/mirror/android/app/ContextImpl.java b/VirtualApp/lib/src/main/java/mirror/android/app/ContextImpl.java index fd495fb39..8718c3247 100644 --- a/VirtualApp/lib/src/main/java/mirror/android/app/ContextImpl.java +++ b/VirtualApp/lib/src/main/java/mirror/android/app/ContextImpl.java @@ -17,4 +17,6 @@ public class ContextImpl { public static RefObject mPackageManager; public static RefMethod getReceiverRestrictedContext; + + public static RefMethod getAttributionSource; }