From 73097c6ddecf2853d1e8db5f97f292bba0b7d242 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 14 Feb 2015 20:09:12 +0100 Subject: [PATCH] Lollipop privacy service Refs #1757 --- src/biz/bokhorst/xprivacy/PrivacyManager.java | 2 +- src/biz/bokhorst/xprivacy/PrivacyService.java | 29 ++- src/biz/bokhorst/xprivacy/XPrivacy.java | 220 +++++++++--------- 3 files changed, 124 insertions(+), 127 deletions(-) diff --git a/src/biz/bokhorst/xprivacy/PrivacyManager.java b/src/biz/bokhorst/xprivacy/PrivacyManager.java index c0d114ea1..c7186f4bc 100644 --- a/src/biz/bokhorst/xprivacy/PrivacyManager.java +++ b/src/biz/bokhorst/xprivacy/PrivacyManager.java @@ -212,7 +212,7 @@ else if (!listRestriction.contains(restrictionName)) mPermission.get(aPermission).add(hook); } } - Util.log(null, Log.WARN, listHook.size() + " hooks"); + //Util.log(null, Log.WARN, listHook.size() + " hooks"); } public static List getRestrictions() { diff --git a/src/biz/bokhorst/xprivacy/PrivacyService.java b/src/biz/bokhorst/xprivacy/PrivacyService.java index b21ce4911..51f6aee89 100644 --- a/src/biz/bokhorst/xprivacy/PrivacyService.java +++ b/src/biz/bokhorst/xprivacy/PrivacyService.java @@ -70,6 +70,7 @@ public class PrivacyService extends IPrivacyService.Stub { private static int mXUid = -1; + private static Context mContext; private static String mSecret = null; private static Thread mWorker = null; private static Handler mHandler = null; @@ -126,7 +127,7 @@ private PrivacyService() { private static PrivacyService mPrivacyService = null; - public static void register(List listError, String secret) { + public static void register(List listError, ClassLoader classLoader, String secret, Object am) { // Store secret and errors mSecret = secret; mListError.addAll(listError); @@ -139,7 +140,11 @@ public static void register(List listError, String secret) { // public static void addService(String name, IBinder service) // public static void addService(String name, IBinder service, boolean allowIsolated) // @formatter:on - Class cServiceManager = Class.forName("android.os.ServiceManager"); + + // Requires this in /service_contexts + // xprivacy453 u:object_r:system_server_service:s0 + + Class cServiceManager = Class.forName("android.os.ServiceManager", false, classLoader); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { Method mAddService = cServiceManager.getDeclaredMethod("addService", String.class, IBinder.class, boolean.class); @@ -155,6 +160,11 @@ public static void register(List listError, String secret) { // Publish semaphore to activity manager service XActivityManagerService.setSemaphore(mOndemandSemaphore); + // Get context + Field fContext = am.getClass().getDeclaredField("mContext"); + fContext.setAccessible(true); + mContext = (Context) fContext.get(am); + // Start a worker thread mWorker = new Thread(new Runnable() { @Override @@ -2270,20 +2280,7 @@ private boolean isAMLocked(int uid) { } private Context getContext() { - // 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; - } + return mContext; } private int getIsolatedUid(int uid) { diff --git a/src/biz/bokhorst/xprivacy/XPrivacy.java b/src/biz/bokhorst/xprivacy/XPrivacy.java index e680a90b5..cabe29f68 100644 --- a/src/biz/bokhorst/xprivacy/XPrivacy.java +++ b/src/biz/bokhorst/xprivacy/XPrivacy.java @@ -100,161 +100,160 @@ private static void init(String path) { } catch (Throwable ex) { Util.bug(null, ex); } + } - // 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, mSecret); - } - }); - } catch (Throwable ex) { - Util.bug(null, ex); - return; - } + private static void handleLoadPackage(String packageName, final ClassLoader classLoader, String secret) { + Util.log(null, Log.INFO, "Load package=" + packageName + " uid=" + Process.myUid()); - // Account manager - hookAll(XAccountManager.getInstances(null), null, mSecret); + if ("android".equals(packageName)) + try { + Class cSystemServer = Class.forName("com.android.server.am.ActivityManagerService", false, + classLoader); + Method mMain = cSystemServer.getDeclaredMethod("setSystemProcess"); + XposedBridge.hookMethod(mMain, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + PrivacyService.register(mListHookError, classLoader, mSecret, param.thisObject); - // Activity manager - hookAll(XActivityManager.getInstances(null), null, mSecret); + // Account manager + hookAll(XAccountManager.getInstances(null), classLoader, mSecret); - // Activity manager service - hookAll(XActivityManagerService.getInstances(), null, mSecret); + // Activity manager + hookAll(XActivityManager.getInstances(null), classLoader, mSecret); - // App widget manager - hookAll(XAppWidgetManager.getInstances(), null, mSecret); + // Activity manager service + hookAll(XActivityManagerService.getInstances(), classLoader, mSecret); - // Application - hookAll(XApplication.getInstances(), null, mSecret); + // App widget manager + hookAll(XAppWidgetManager.getInstances(), classLoader, mSecret); - // Audio record - hookAll(XAudioRecord.getInstances(), null, mSecret); + // Application + hookAll(XApplication.getInstances(), classLoader, mSecret); - // Binder device - hookAll(XBinder.getInstances(), null, mSecret); + // Audio record + hookAll(XAudioRecord.getInstances(), classLoader, mSecret); - // Bluetooth adapater - hookAll(XBluetoothAdapter.getInstances(), null, mSecret); + // Binder device + hookAll(XBinder.getInstances(), classLoader, mSecret); - // Bluetooth device - hookAll(XBluetoothDevice.getInstances(), null, mSecret); + // Bluetooth adapater + hookAll(XBluetoothAdapter.getInstances(), classLoader, mSecret); - // Camera - hookAll(XCamera.getInstances(), null, mSecret); + // Bluetooth device + hookAll(XBluetoothDevice.getInstances(), classLoader, mSecret); - // Camera2 device - hookAll(XCameraDevice2.getInstances(), null, mSecret); + // Camera + hookAll(XCamera.getInstances(), classLoader, mSecret); - // Clipboard manager - hookAll(XClipboardManager.getInstances(null), null, mSecret); + // Camera2 device + hookAll(XCameraDevice2.getInstances(), classLoader, mSecret); - // Connectivity manager - hookAll(XConnectivityManager.getInstances(null), null, mSecret); + // Clipboard manager + hookAll(XClipboardManager.getInstances(null), classLoader, mSecret); - // Content resolver - hookAll(XContentResolver.getInstances(null), null, mSecret); + // Connectivity manager + hookAll(XConnectivityManager.getInstances(null), classLoader, mSecret); - // Context wrapper - hookAll(XContextImpl.getInstances(), null, mSecret); + // Content resolver + hookAll(XContentResolver.getInstances(null), classLoader, mSecret); - // Environment - hookAll(XEnvironment.getInstances(), null, mSecret); + // Context wrapper + hookAll(XContextImpl.getInstances(), classLoader, mSecret); - // Inet address - hookAll(XInetAddress.getInstances(), null, mSecret); + // Environment + hookAll(XEnvironment.getInstances(), classLoader, mSecret); - // Input device - hookAll(XInputDevice.getInstances(), null, mSecret); + // Inet address + hookAll(XInetAddress.getInstances(), classLoader, mSecret); - // Intent firewall - hookAll(XIntentFirewall.getInstances(), null, mSecret); + // Input device + hookAll(XInputDevice.getInstances(), classLoader, mSecret); - // IO bridge - hookAll(XIoBridge.getInstances(), null, mSecret); + // Intent firewall + hookAll(XIntentFirewall.getInstances(), classLoader, mSecret); - // IP prefix - hookAll(XIpPrefix.getInstances(), null, mSecret); + // IO bridge + hookAll(XIoBridge.getInstances(), classLoader, mSecret); - // Link properties - hookAll(XLinkProperties.getInstances(), null, mSecret); + // IP prefix + hookAll(XIpPrefix.getInstances(), classLoader, mSecret); - // Location manager - hookAll(XLocationManager.getInstances(null), null, mSecret); + // Link properties + hookAll(XLinkProperties.getInstances(), classLoader, mSecret); - // Media recorder - hookAll(XMediaRecorder.getInstances(), null, mSecret); + // Location manager + hookAll(XLocationManager.getInstances(null), classLoader, mSecret); - // Network info - hookAll(XNetworkInfo.getInstances(), null, mSecret); + // Media recorder + hookAll(XMediaRecorder.getInstances(), classLoader, mSecret); - // Network interface - hookAll(XNetworkInterface.getInstances(), null, mSecret); + // Network info + hookAll(XNetworkInfo.getInstances(), classLoader, mSecret); - // NFC adapter - hookAll(XNfcAdapter.getInstances(), null, mSecret); + // Network interface + hookAll(XNetworkInterface.getInstances(), classLoader, mSecret); - // Package manager service - hookAll(XPackageManager.getInstances(null), null, mSecret); + // NFC adapter + hookAll(XNfcAdapter.getInstances(), classLoader, mSecret); - // Process - hookAll(XProcess.getInstances(), null, mSecret); + // Package manager service + hookAll(XPackageManager.getInstances(null), classLoader, mSecret); - // Process builder - hookAll(XProcessBuilder.getInstances(), null, mSecret); + // Process + hookAll(XProcess.getInstances(), classLoader, mSecret); - // Resources - hookAll(XResources.getInstances(), null, mSecret); + // Process builder + hookAll(XProcessBuilder.getInstances(), classLoader, mSecret); - // Runtime - hookAll(XRuntime.getInstances(), null, mSecret); + // Resources + hookAll(XResources.getInstances(), classLoader, mSecret); - // Sensor manager - hookAll(XSensorManager.getInstances(null), null, mSecret); + // Runtime + hookAll(XRuntime.getInstances(), classLoader, mSecret); - // Settings secure - hookAll(XSettingsSecure.getInstances(), null, mSecret); + // Sensor manager + hookAll(XSensorManager.getInstances(null), classLoader, mSecret); - // SIP manager - hookAll(XSipManager.getInstances(), null, mSecret); + // Settings secure + hookAll(XSettingsSecure.getInstances(), classLoader, mSecret); - // SMS manager - hookAll(XSmsManager.getInstances(), null, mSecret); + // SIP manager + hookAll(XSipManager.getInstances(), classLoader, mSecret); - // System properties - hookAll(XSystemProperties.getInstances(), null, mSecret); + // SMS manager + hookAll(XSmsManager.getInstances(), classLoader, mSecret); - // Telephone service - hookAll(XTelephonyManager.getInstances(null), null, mSecret); + // System properties + hookAll(XSystemProperties.getInstances(), classLoader, mSecret); - // Usage statistics manager - hookAll(XUsageStatsManager.getInstances(), null, mSecret); + // Telephone service + hookAll(XTelephonyManager.getInstances(null), classLoader, mSecret); - // USB device - hookAll(XUsbDevice.getInstances(), null, mSecret); + // Usage statistics manager + hookAll(XUsageStatsManager.getInstances(), classLoader, mSecret); - // Web view - hookAll(XWebView.getInstances(), null, mSecret); + // USB device + hookAll(XUsbDevice.getInstances(), classLoader, mSecret); - // Window service - hookAll(XWindowManager.getInstances(null), null, mSecret); + // Web view + hookAll(XWebView.getInstances(), classLoader, mSecret); - // Wi-Fi service - hookAll(XWifiManager.getInstances(null), null, mSecret); + // Window service + hookAll(XWindowManager.getInstances(null), classLoader, mSecret); - // Intent receive - hookAll(XActivityThread.getInstances(), null, mSecret); + // Wi-Fi service + hookAll(XWifiManager.getInstances(null), classLoader, mSecret); - // Intent send - hookAll(XActivity.getInstances(), null, mSecret); - } + // Intent receive + hookAll(XActivityThread.getInstances(), classLoader, mSecret); - private static void handleLoadPackage(String packageName, ClassLoader classLoader, String secret) { - Util.log(null, Log.INFO, "Load package=" + packageName + " uid=" + Process.myUid()); + // Intent send + hookAll(XActivity.getInstances(), classLoader, mSecret); + } + }); + } catch (Throwable ex) { + Util.bug(null, ex); + } // Skip hooking self String self = XPrivacy.class.getPackage().getName(); @@ -407,7 +406,8 @@ private static void hook(final XHook hook, ClassLoader classLoader, String secre if (level == Log.ERROR) mListHookError.add(message); Util.log(hook, level, message); - Util.logStack(hook, level); + // Util.logStack(hook, level); + return; } // Get members @@ -458,7 +458,7 @@ private static void hook(final XHook hook, ClassLoader classLoader, String secre if (level == Log.ERROR) mListHookError.add(message); Util.log(hook, level, message); - Util.logStack(hook, level); + // Util.logStack(hook, level); } } catch (Throwable ex) { mListHookError.add(ex.toString());