diff options
author | Alisher Alikhodjaev <alisher@google.com> | 2024-04-23 18:44:08 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-04-23 18:44:08 +0000 |
commit | aad0bb15930e932c64baec5b976dbe0b371835fb (patch) | |
tree | e8d64bd32aef5f32767e4692c81d2e3de012435b | |
parent | 9e486a78b2f4ff772844324586ce9a08b359f617 (diff) | |
parent | 692b23d45774d2c335da19b359c36acd2d8c37d5 (diff) | |
download | Nfc-aad0bb15930e932c64baec5b976dbe0b371835fb.tar.gz |
Merge "An unpriveledged app can change discovery mask in BG." into main
-rw-r--r-- | src/com/android/nfc/NfcService.java | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java index 8456ea10..0a913f3e 100644 --- a/src/com/android/nfc/NfcService.java +++ b/src/com/android/nfc/NfcService.java @@ -318,6 +318,7 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback NfcDiscoveryParameters.getNfcOffParameters(); ReaderModeParams mReaderModeParams; + DiscoveryTechParams mDiscoveryTechParams; private int mUserId; boolean mPollingPaused; @@ -536,6 +537,11 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback public int uid; } + final class DiscoveryTechParams { + public IBinder binder; + public int uid; + } + void saveNfcOnSetting(boolean on) { synchronized (NfcService.this) { mPrefsEditor.putBoolean(PREF_NFC_ON, on); @@ -1244,6 +1250,7 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback mHandler.removeMessages(MSG_DELAY_POLLING); mPollingDisableDeathRecipients.clear(); mReaderModeParams = null; + mDiscoveryTechParams = null; } mNfcDispatcher.setForegroundDispatch(null, null, null); @@ -1427,6 +1434,16 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback resetReaderModeParams(); } } + synchronized (NfcService.this) { + if (mDiscoveryTechParams != null && mDiscoveryTechParams.uid == uid) { + mDiscoveryTechParams.binder.unlinkToDeath(mDiscoveryTechDeathRecipient, 0); + mDeviceHost.resetDiscoveryTech(); + mDiscoveryTechParams = null; + if (isNfcEnabled()) { + applyRouting(true); + } + } + } } public boolean isSecureNfcEnabled() { @@ -1713,6 +1730,23 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback public void updateDiscoveryTechnology(IBinder binder, int pollTech, int listenTech) throws RemoteException { NfcPermissions.enforceUserPermissions(mContext); + int callingUid = Binder.getCallingUid(); + boolean privilegedCaller = false; + // Allow non-foreground callers with system uid or systemui + String packageName = getPackageNameFromUid(callingUid); + if (packageName != null) { + privilegedCaller = (callingUid == Process.SYSTEM_UID + || packageName.equals(SYSTEM_UI)); + } else { + privilegedCaller = (callingUid == Process.SYSTEM_UID); + } + if (!privilegedCaller + && !mForegroundUtils.registerUidToBackgroundCallback( + NfcService.this, callingUid)) { + Log.e(TAG, + "updateDiscoveryTechnology: Caller shall be in foreground or a system process."); + return; + } synchronized (NfcService.this) { if (!isNfcEnabled()) { Log.d(TAG, "updateDiscoveryTechnology: NFC is not enabled."); @@ -1723,16 +1757,21 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback Integer.toHexString(pollTech) + ", listenTech: 0x" + Integer.toHexString(listenTech)); if (pollTech == NfcAdapter.FLAG_USE_ALL_TECH && - listenTech == NfcAdapter.FLAG_USE_ALL_TECH) { + listenTech == NfcAdapter.FLAG_USE_ALL_TECH && + mDiscoveryTechParams != null) { try { - mDeviceHost.resetDiscoveryTech(); binder.unlinkToDeath(mDiscoveryTechDeathRecipient, 0); + mDeviceHost.resetDiscoveryTech(); + mDiscoveryTechParams = null; } catch (NoSuchElementException e) { Log.e(TAG, "Change Tech Binder was never registered."); } } else { try { mDeviceHost.setDiscoveryTech(pollTech, listenTech); + mDiscoveryTechParams = new DiscoveryTechParams(); + mDiscoveryTechParams.uid = callingUid; + mDiscoveryTechParams.binder = binder; binder.linkToDeath(mDiscoveryTechDeathRecipient, 0); } catch (RemoteException e) { Log.e(TAG, "Remote binder has already died."); @@ -2212,13 +2251,14 @@ public class NfcService implements DeviceHostListener, ForegroundUtils.Callback final class DiscoveryTechDeathRecipient implements IBinder.DeathRecipient { @Override public void binderDied() { + if (DBG) Log.d(TAG, "setDiscoveryTech death recipient"); synchronized (NfcService.this) { - if (isNfcEnabled()) { - if (DBG) Log.d(TAG, "setDiscoveryTech death recipient"); + if (isNfcEnabled() && mDiscoveryTechParams != null) { mDeviceHost.resetDiscoveryTech(); - applyRouting(true); + mDiscoveryTechParams = null; } } + applyRouting(true); } } |