Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79e2a809 authored by Jayden Kim's avatar Jayden Kim Committed by Automerger Merge Worker
Browse files

Merge "Apply uid of original caller when starting LE scan by PendingIntent"...

Merge "Apply uid of original caller when starting LE scan by PendingIntent" into main am: 92dc7c29

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2749702



Change-Id: I3bb186d4e8f2fd9077bb57e7b600aefd47b3cc75
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 0af8c7a3 92dc7c29
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -209,12 +209,22 @@ public class ContextMap<C, T> {
    private Set<Connection> mConnections = new HashSet<Connection>();
    private final Object mConnectionsLock = new Object();

    /**
     * Add an entry to the application context list.
     */
    App add(UUID uuid, WorkSource workSource, C callback, T info, GattService service) {
        int appUid = Binder.getCallingUid();
        String appName = service.getPackageManager().getNameForUid(appUid);
    /** Add an entry to the application context list. */
    App add(
            UUID uuid,
            WorkSource workSource,
            C callback,
            GattService.PendingIntentInfo piInfo,
            GattService service) {
        int appUid;
        String appName = null;
        if (piInfo != null) {
            appUid = piInfo.callingUid;
            appName = piInfo.callingPackage;
        } else {
            appUid = Binder.getCallingUid();
            appName = service.getPackageManager().getNameForUid(appUid);
        }
        if (appName == null) {
            // Assign an app name if one isn't found
            appName = "Unknown App (UID: " + appUid + ")";
@@ -225,7 +235,7 @@ public class ContextMap<C, T> {
                appScanStats = new AppScanStats(appName, workSource, this, service);
                mAppScanStats.put(appUid, appScanStats);
            }
            App app = new App(uuid, callback, info, appName, appScanStats);
            App app = new App(uuid, callback, (T) piInfo, appName, appScanStats);
            mApps.add(app);
            appScanStats.isRegistered = true;
            return app;
+11 −3
Original line number Diff line number Diff line
@@ -208,6 +208,7 @@ public class GattService extends ProfileService {
        public ScanSettings settings;
        public List<ScanFilter> filters;
        public String callingPackage;
        public int callingUid;

        @Override
        public boolean equals(Object other) {
@@ -3257,15 +3258,22 @@ public class GattService extends ProfileService {
        settings = enforceReportDelayFloor(settings);
        enforcePrivilegedPermissionIfNeeded(filters);
        UUID uuid = UUID.randomUUID();
        String callingPackage = attributionSource.getPackageName();
        int callingUid = attributionSource.getUid();
        if (DBG) {
            Log.d(TAG, "startScan(PI) - UUID=" + uuid);
            Log.d(
                    TAG,
                    "startScan(PI) -"
                            + (" UUID=" + uuid)
                            + (" Package=" + callingPackage)
                            + (" UID=" + callingUid));
        }
        String callingPackage = attributionSource.getPackageName();
        PendingIntentInfo piInfo = new PendingIntentInfo();
        piInfo.intent = pendingIntent;
        piInfo.settings = settings;
        piInfo.filters = filters;
        piInfo.callingPackage = callingPackage;
        piInfo.callingUid = callingUid;

        // Don't start scan if the Pi scan already in mScannerMap.
        if (mScannerMap.getByContextInfo(piInfo) != null) {
@@ -3316,7 +3324,7 @@ public class GattService extends ProfileService {
    void continuePiStartScan(int scannerId, ScannerMap.App app) {
        final PendingIntentInfo piInfo = app.info;
        final ScanClient scanClient =
                new ScanClient(scannerId, piInfo.settings, piInfo.filters);
                new ScanClient(scannerId, piInfo.settings, piInfo.filters, piInfo.callingUid);
        scanClient.hasLocationPermission = app.hasLocationPermission;
        scanClient.userHandle = app.mUserHandle;
        scanClient.isQApp = checkCallerTargetSdk(this, app.name, Build.VERSION_CODES.Q);
+5 −1
Original line number Diff line number Diff line
@@ -58,11 +58,15 @@ import java.util.Objects;
    }

    ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters) {
        this(scannerId, settings, filters, Binder.getCallingUid());
    }

    ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters, int appUid) {
        this.scannerId = scannerId;
        this.settings = settings;
        this.scanModeApp = settings.getScanMode();
        this.filters = filters;
        this.appUid = Binder.getCallingUid();
        this.appUid = appUid;
    }

    @Override
+13 −0
Original line number Diff line number Diff line
@@ -387,6 +387,8 @@ public class ScanManager {
                Log.d(TAG, "handling starting scan");
            }

            fetchAppForegroundState(client);

            if (!isScanSupported(client)) {
                Log.e(TAG, "Scan settings not supported");
                return;
@@ -732,6 +734,17 @@ public class ScanManager {
            return mIsUidForegroundMap.get(client.appUid, DEFAULT_UID_IS_FOREGROUND);
        }

        private void fetchAppForegroundState(ScanClient client) {
            if (mActivityManager == null) {
                return;
            }
            int importance = mActivityManager.getUidImportance(client.appUid);
            boolean isForeground =
                    importance
                            <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
            mIsUidForegroundMap.put(client.appUid, isForeground);
        }

        private boolean updateScanModeBeforeStart(ScanClient client) {
            if (upgradeScanModeBeforeStart(client)) {
                return true;
+27 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -262,6 +263,32 @@ public class GattServiceTest {
        verify(mScanManager).startScan(any());
    }

    @Test
    public void continuePiStartScanCheckUid() {
        int scannerId = 1;

        mPiInfo.settings = new ScanSettings.Builder().build();
        mPiInfo.callingUid = 123;
        mApp.info = mPiInfo;

        AppScanStats appScanStats = mock(AppScanStats.class);
        doReturn(appScanStats).when(mScannerMap).getAppScanStatsById(scannerId);

        mService.continuePiStartScan(scannerId, mApp);

        verify(appScanStats)
                .recordScanStart(mPiInfo.settings, mPiInfo.filters, false, false, scannerId);
        verify(mScanManager)
                .startScan(
                        argThat(
                                new ArgumentMatcher<ScanClient>() {
                                    @Override
                                    public boolean matches(ScanClient client) {
                                        return mPiInfo.callingUid == client.appUid;
                                    }
                                }));
    }

    @Test
    public void onBatchScanReportsInternal_deliverBatchScan() throws RemoteException {
        int status = 1;