From 09832e91e21cd77425c43cc0cc464f460d2f3df3 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 14 Feb 2015 21:47:59 +0100 Subject: [PATCH] Backward compatiblity, fixes Refs #1757 --- src/biz/bokhorst/xprivacy/PrivacyService.java | 27 +- src/biz/bokhorst/xprivacy/XPrivacy.java | 303 ++++++++++-------- 2 files changed, 187 insertions(+), 143 deletions(-) diff --git a/src/biz/bokhorst/xprivacy/PrivacyService.java b/src/biz/bokhorst/xprivacy/PrivacyService.java index 51f6aee89..d0be20bfc 100644 --- a/src/biz/bokhorst/xprivacy/PrivacyService.java +++ b/src/biz/bokhorst/xprivacy/PrivacyService.java @@ -161,9 +161,11 @@ public static void register(List listError, ClassLoader classLoader, Str XActivityManagerService.setSemaphore(mOndemandSemaphore); // Get context - Field fContext = am.getClass().getDeclaredField("mContext"); - fContext.setAccessible(true); - mContext = (Context) fContext.get(am); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Field fContext = am.getClass().getDeclaredField("mContext"); + fContext.setAccessible(true); + mContext = (Context) fContext.get(am); + } // Start a worker thread mWorker = new Thread(new Runnable() { @@ -2280,7 +2282,24 @@ private boolean isAMLocked(int uid) { } private Context getContext() { - return mContext; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + return mContext; + else { + // public static ActivityManagerService self() + // frameworks/base/services/java/com/android/server/am/ActivityManagerService.java + try { + Class cam = Class.forName("com.android.server.am.ActivityManagerService"); + Object am = cam.getMethod("self").invoke(null); + if (am == null) + return null; + Field mContext = cam.getDeclaredField("mContext"); + mContext.setAccessible(true); + return (Context) mContext.get(am); + } catch (Throwable ex) { + Util.bug(null, ex); + return null; + } + } } private int getIsolatedUid(int uid) { diff --git a/src/biz/bokhorst/xprivacy/XPrivacy.java b/src/biz/bokhorst/xprivacy/XPrivacy.java index cabe29f68..4d8379d42 100644 --- a/src/biz/bokhorst/xprivacy/XPrivacy.java +++ b/src/biz/bokhorst/xprivacy/XPrivacy.java @@ -100,12 +100,33 @@ private static void init(String path) { } catch (Throwable ex) { Util.bug(null, ex); } + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + // System server + try { + // frameworks/base/services/java/com/android/server/SystemServer.java + Class cSystemServer = Class.forName("com.android.server.SystemServer"); + Method mMain = cSystemServer.getDeclaredMethod("main", String[].class); + XposedBridge.hookMethod(mMain, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + PrivacyService.register(mListHookError, null, mSecret, null); + } + }); + } catch (Throwable ex) { + Util.bug(null, ex); + return; + } + + hookAll(null); + } } private static void handleLoadPackage(String packageName, final ClassLoader classLoader, String secret) { - Util.log(null, Log.INFO, "Load package=" + packageName + " uid=" + Process.myUid()); + // Util.log(null, Log.INFO, "Load package=" + packageName + " uid=" + + // Process.myUid()); - if ("android".equals(packageName)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && "android".equals(packageName)) try { Class cSystemServer = Class.forName("com.android.server.am.ActivityManagerService", false, classLoader); @@ -114,141 +135,7 @@ private static void handleLoadPackage(String packageName, final ClassLoader clas @Override protected void beforeHookedMethod(MethodHookParam param) throws Throwable { PrivacyService.register(mListHookError, classLoader, mSecret, param.thisObject); - - // Account manager - hookAll(XAccountManager.getInstances(null), classLoader, mSecret); - - // Activity manager - hookAll(XActivityManager.getInstances(null), classLoader, mSecret); - - // Activity manager service - hookAll(XActivityManagerService.getInstances(), classLoader, mSecret); - - // App widget manager - hookAll(XAppWidgetManager.getInstances(), classLoader, mSecret); - - // Application - hookAll(XApplication.getInstances(), classLoader, mSecret); - - // Audio record - hookAll(XAudioRecord.getInstances(), classLoader, mSecret); - - // Binder device - hookAll(XBinder.getInstances(), classLoader, mSecret); - - // Bluetooth adapater - hookAll(XBluetoothAdapter.getInstances(), classLoader, mSecret); - - // Bluetooth device - hookAll(XBluetoothDevice.getInstances(), classLoader, mSecret); - - // Camera - hookAll(XCamera.getInstances(), classLoader, mSecret); - - // Camera2 device - hookAll(XCameraDevice2.getInstances(), classLoader, mSecret); - - // Clipboard manager - hookAll(XClipboardManager.getInstances(null), classLoader, mSecret); - - // Connectivity manager - hookAll(XConnectivityManager.getInstances(null), classLoader, mSecret); - - // Content resolver - hookAll(XContentResolver.getInstances(null), classLoader, mSecret); - - // Context wrapper - hookAll(XContextImpl.getInstances(), classLoader, mSecret); - - // Environment - hookAll(XEnvironment.getInstances(), classLoader, mSecret); - - // Inet address - hookAll(XInetAddress.getInstances(), classLoader, mSecret); - - // Input device - hookAll(XInputDevice.getInstances(), classLoader, mSecret); - - // Intent firewall - hookAll(XIntentFirewall.getInstances(), classLoader, mSecret); - - // IO bridge - hookAll(XIoBridge.getInstances(), classLoader, mSecret); - - // IP prefix - hookAll(XIpPrefix.getInstances(), classLoader, mSecret); - - // Link properties - hookAll(XLinkProperties.getInstances(), classLoader, mSecret); - - // Location manager - hookAll(XLocationManager.getInstances(null), classLoader, mSecret); - - // Media recorder - hookAll(XMediaRecorder.getInstances(), classLoader, mSecret); - - // Network info - hookAll(XNetworkInfo.getInstances(), classLoader, mSecret); - - // Network interface - hookAll(XNetworkInterface.getInstances(), classLoader, mSecret); - - // NFC adapter - hookAll(XNfcAdapter.getInstances(), classLoader, mSecret); - - // Package manager service - hookAll(XPackageManager.getInstances(null), classLoader, mSecret); - - // Process - hookAll(XProcess.getInstances(), classLoader, mSecret); - - // Process builder - hookAll(XProcessBuilder.getInstances(), classLoader, mSecret); - - // Resources - hookAll(XResources.getInstances(), classLoader, mSecret); - - // Runtime - hookAll(XRuntime.getInstances(), classLoader, mSecret); - - // Sensor manager - hookAll(XSensorManager.getInstances(null), classLoader, mSecret); - - // Settings secure - hookAll(XSettingsSecure.getInstances(), classLoader, mSecret); - - // SIP manager - hookAll(XSipManager.getInstances(), classLoader, mSecret); - - // SMS manager - hookAll(XSmsManager.getInstances(), classLoader, mSecret); - - // System properties - hookAll(XSystemProperties.getInstances(), classLoader, mSecret); - - // Telephone service - hookAll(XTelephonyManager.getInstances(null), classLoader, mSecret); - - // Usage statistics manager - hookAll(XUsageStatsManager.getInstances(), classLoader, mSecret); - - // USB device - hookAll(XUsbDevice.getInstances(), classLoader, mSecret); - - // Web view - hookAll(XWebView.getInstances(), classLoader, mSecret); - - // Window service - hookAll(XWindowManager.getInstances(null), classLoader, mSecret); - - // Wi-Fi service - hookAll(XWifiManager.getInstances(null), classLoader, mSecret); - - // Intent receive - hookAll(XActivityThread.getInstances(), classLoader, mSecret); - - // Intent send - hookAll(XActivity.getInstances(), classLoader, mSecret); + hookAll(classLoader); } }); } catch (Throwable ex) { @@ -263,8 +150,9 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable { } // Build SERIAL - if (PrivacyManager.getRestrictionExtra(null, Process.myUid(), PrivacyManager.cIdentification, "SERIAL", null, - Build.SERIAL, secret)) + if (Process.myUid() != Process.SYSTEM_UID + && PrivacyManager.getRestrictionExtra(null, Process.myUid(), PrivacyManager.cIdentification, "SERIAL", + null, Build.SERIAL, secret)) try { Field serial = Build.class.getField("SERIAL"); serial.setAccessible(true); @@ -337,6 +225,143 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable { hookAll(XContentResolver.getPackageInstances(packageName, classLoader), classLoader, secret); } + private static void hookAll(final ClassLoader classLoader) { + // Account manager + hookAll(XAccountManager.getInstances(null), classLoader, mSecret); + + // Activity manager + hookAll(XActivityManager.getInstances(null), classLoader, mSecret); + + // Activity manager service + hookAll(XActivityManagerService.getInstances(), classLoader, mSecret); + + // App widget manager + hookAll(XAppWidgetManager.getInstances(), classLoader, mSecret); + + // Application + hookAll(XApplication.getInstances(), classLoader, mSecret); + + // Audio record + hookAll(XAudioRecord.getInstances(), classLoader, mSecret); + + // Binder device + hookAll(XBinder.getInstances(), classLoader, mSecret); + + // Bluetooth adapater + hookAll(XBluetoothAdapter.getInstances(), classLoader, mSecret); + + // Bluetooth device + hookAll(XBluetoothDevice.getInstances(), classLoader, mSecret); + + // Camera + hookAll(XCamera.getInstances(), classLoader, mSecret); + + // Camera2 device + hookAll(XCameraDevice2.getInstances(), classLoader, mSecret); + + // Clipboard manager + hookAll(XClipboardManager.getInstances(null), classLoader, mSecret); + + // Connectivity manager + hookAll(XConnectivityManager.getInstances(null), classLoader, mSecret); + + // Content resolver + hookAll(XContentResolver.getInstances(null), classLoader, mSecret); + + // Context wrapper + hookAll(XContextImpl.getInstances(), classLoader, mSecret); + + // Environment + hookAll(XEnvironment.getInstances(), classLoader, mSecret); + + // Inet address + hookAll(XInetAddress.getInstances(), classLoader, mSecret); + + // Input device + hookAll(XInputDevice.getInstances(), classLoader, mSecret); + + // Intent firewall + hookAll(XIntentFirewall.getInstances(), classLoader, mSecret); + + // IO bridge + hookAll(XIoBridge.getInstances(), classLoader, mSecret); + + // IP prefix + hookAll(XIpPrefix.getInstances(), classLoader, mSecret); + + // Link properties + hookAll(XLinkProperties.getInstances(), classLoader, mSecret); + + // Location manager + hookAll(XLocationManager.getInstances(null), classLoader, mSecret); + + // Media recorder + hookAll(XMediaRecorder.getInstances(), classLoader, mSecret); + + // Network info + hookAll(XNetworkInfo.getInstances(), classLoader, mSecret); + + // Network interface + hookAll(XNetworkInterface.getInstances(), classLoader, mSecret); + + // NFC adapter + hookAll(XNfcAdapter.getInstances(), classLoader, mSecret); + + // Package manager service + hookAll(XPackageManager.getInstances(null), classLoader, mSecret); + + // Process + hookAll(XProcess.getInstances(), classLoader, mSecret); + + // Process builder + hookAll(XProcessBuilder.getInstances(), classLoader, mSecret); + + // Resources + hookAll(XResources.getInstances(), classLoader, mSecret); + + // Runtime + hookAll(XRuntime.getInstances(), classLoader, mSecret); + + // Sensor manager + hookAll(XSensorManager.getInstances(null), classLoader, mSecret); + + // Settings secure + hookAll(XSettingsSecure.getInstances(), classLoader, mSecret); + + // SIP manager + hookAll(XSipManager.getInstances(), classLoader, mSecret); + + // SMS manager + hookAll(XSmsManager.getInstances(), classLoader, mSecret); + + // System properties + hookAll(XSystemProperties.getInstances(), classLoader, mSecret); + + // Telephone service + hookAll(XTelephonyManager.getInstances(null), classLoader, mSecret); + + // Usage statistics manager + hookAll(XUsageStatsManager.getInstances(), classLoader, mSecret); + + // USB device + hookAll(XUsbDevice.getInstances(), classLoader, mSecret); + + // Web view + hookAll(XWebView.getInstances(), classLoader, mSecret); + + // Window service + hookAll(XWindowManager.getInstances(null), classLoader, mSecret); + + // Wi-Fi service + hookAll(XWifiManager.getInstances(null), classLoader, mSecret); + + // Intent receive + hookAll(XActivityThread.getInstances(), classLoader, mSecret); + + // Intent send + hookAll(XActivity.getInstances(), classLoader, mSecret); + } + public static void handleGetSystemService(String name, String className, String secret) { if (PrivacyManager.getTransient(className, null) == null) { PrivacyManager.setTransient(className, Boolean.toString(true));