From 21f962585d4d1cd218b4d29bc5b436f13e48a265 Mon Sep 17 00:00:00 2001 From: Ye Jiao Date: Mon, 28 Jan 2019 12:54:22 +0800 Subject: [PATCH] Fix getWifiManager exception if phone is encrypted [Issue Details] NetworkPolicyManagerService may invoke dispatchRestrictBackgroundChanged after reboot if settings changed, and invokes untetherAll in turn, which triggers ServiceNotFoundException indirectly if the device is under encryption. The cause is that WifiService is not running if the device is still locked. [Solution] In order to avoid ServiceNotFoundException in setWifiTethering, we add check on return value of getWifiManager. Moreover, check of UsbManager is also added to prevent potential similar issues of USB tethering. Bug: 123504004 Test: FrameworksNetTests adb shell am instrument -w com.android.frameworks.tests.net Test: CTS - CtsNetTestCases ANDROID_BUILD_droid-cts/tools/cts-tradefed run cts -m CtsNetTestCases Test: CTS - CtsNetTestCasesLegacyApi22 ANDROID_BUILD_droid-cts/tools/cts-tradefed run cts -m CtsNetTestCasesLegacyApi22 Test: CTS - CtsNetTestCasesLegacyPermission22 ANDROID_BUILD_droid-cts/tools/cts-tradefed run cts -m CtsNetTestCasesLegacyPermission22 Change-Id: I2643188b51900f97ba58c8614c66cb55ebfaa2f6 --- .../android/server/connectivity/Tethering.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java index 35704d404ff32..6491cecdbed63 100644 --- a/services/core/java/com/android/server/connectivity/Tethering.java +++ b/services/core/java/com/android/server/connectivity/Tethering.java @@ -423,21 +423,25 @@ public class Tethering extends BaseNetworkObserver { } private int setWifiTethering(final boolean enable) { - int rval = TETHER_ERROR_MASTER_ERROR; final long ident = Binder.clearCallingIdentity(); try { synchronized (mPublicSync) { - mWifiTetherRequested = enable; final WifiManager mgr = getWifiManager(); + if (mgr == null) { + mLog.e("setWifiTethering: failed to get WifiManager!"); + return TETHER_ERROR_SERVICE_UNAVAIL; + } if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) || (!enable && mgr.stopSoftAp())) { - rval = TETHER_ERROR_NO_ERROR; + mWifiTetherRequested = enable; + return TETHER_ERROR_NO_ERROR; } } } finally { Binder.restoreCallingIdentity(ident); } - return rval; + + return TETHER_ERROR_MASTER_ERROR; } private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) { @@ -989,6 +993,11 @@ public class Tethering extends BaseNetworkObserver { public int setUsbTethering(boolean enable) { if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); + if (usbManager == null) { + mLog.e("setUsbTethering: failed to get UsbManager!"); + return TETHER_ERROR_SERVICE_UNAVAIL; + } + synchronized (mPublicSync) { usbManager.setCurrentFunctions(enable ? UsbManager.FUNCTION_RNDIS : UsbManager.FUNCTION_NONE);