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

Commit 93821176 authored by William Escande's avatar William Escande
Browse files

SystemServer: use own attribution when calling app

Test: atest boot test with the CL to fix the synchronous
Flag: Exempt, must have for aosp/3009913 that cannot be flagged
Bug: 324420709
Change-Id: I299b3a26880c74b3bb02bda8a75e1b1512b4c093
parent 879a70e5
Loading
Loading
Loading
Loading
+14 −15
Original line number Original line Diff line number Diff line
@@ -41,7 +41,6 @@ import android.bluetooth.IBluetooth;
import android.bluetooth.IBluetoothCallback;
import android.bluetooth.IBluetoothCallback;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothManagerCallback;
import android.bluetooth.IBluetoothManagerCallback;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentResolver;
@@ -326,7 +325,7 @@ class BluetoothManagerService {
        }
        }
    }
    }


    boolean onFactoryReset(AttributionSource source) {
    boolean onFactoryReset() {
        // Wait for stable state if bluetooth is temporary state.
        // Wait for stable state if bluetooth is temporary state.
        int state = getState();
        int state = getState();
        if (state == STATE_BLE_TURNING_ON
        if (state == STATE_BLE_TURNING_ON
@@ -350,14 +349,14 @@ class BluetoothManagerService {
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(),
                        mContext.getPackageName(),
                        false);
                        false);
                mAdapter.stopBle(source);
                mAdapter.stopBle(mContext.getAttributionSource());
                return true;
                return true;
            } else if (state == STATE_ON) {
            } else if (state == STATE_ON) {
                addActiveLog(
                addActiveLog(
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
                        mContext.getPackageName(),
                        mContext.getPackageName(),
                        false);
                        false);
                mAdapter.disable(source);
                mAdapter.disable(mContext.getAttributionSource());
                return true;
                return true;
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
@@ -1074,10 +1073,10 @@ class BluetoothManagerService {
        return true;
        return true;
    }
    }


    boolean disableBle(AttributionSource source, String packageName, IBinder token) {
    boolean disableBle(String packageName, IBinder token) {
        Log.i(
        Log.i(
                TAG,
                TAG,
                ("disableBle(" + source + ", " + packageName + ", " + token + "):")
                ("disableBle(" + packageName + ", " + token + "):")
                        + (" mAdapter=" + mAdapter)
                        + (" mAdapter=" + mAdapter)
                        + (" isBinding=" + isBinding())
                        + (" isBinding=" + isBinding())
                        + (" mState=" + mState));
                        + (" mState=" + mState));
@@ -1103,7 +1102,7 @@ class BluetoothManagerService {
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                        BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                        packageName,
                        packageName,
                        false);
                        false);
                sendBrEdrDownCallback(source);
                sendBrEdrDownCallback();
            }
            }
        }
        }
        return true;
        return true;
@@ -1163,7 +1162,7 @@ class BluetoothManagerService {
                android.Manifest.permission.BLUETOOTH_CONNECT,
                android.Manifest.permission.BLUETOOTH_CONNECT,
                android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                android.Manifest.permission.BLUETOOTH_PRIVILEGED,
            })
            })
    private void sendBrEdrDownCallback(AttributionSource source) {
    private void sendBrEdrDownCallback() {
        mAdapterLock.readLock().lock();
        mAdapterLock.readLock().lock();
        try {
        try {
            if (mAdapter == null) {
            if (mAdapter == null) {
@@ -1173,10 +1172,10 @@ class BluetoothManagerService {
            if (isBleAppPresent()) {
            if (isBleAppPresent()) {
                // Need to stay at BLE ON. Disconnect all Gatt connections
                // Need to stay at BLE ON. Disconnect all Gatt connections
                Log.i(TAG, "sendBrEdrDownCallback: Staying in BLE_ON");
                Log.i(TAG, "sendBrEdrDownCallback: Staying in BLE_ON");
                mAdapter.unregAllGattClient(source);
                mAdapter.unregAllGattClient(mContext.getAttributionSource());
            } else {
            } else {
                Log.i(TAG, "sendBrEdrDownCallback: Stopping ble");
                Log.i(TAG, "sendBrEdrDownCallback: Stopping ble");
                mAdapter.stopBle(source);
                mAdapter.stopBle(mContext.getAttributionSource());
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(TAG, "sendBrEdrDownCallback: Call to mAdapter failed.", e);
            Log.e(TAG, "sendBrEdrDownCallback: Call to mAdapter failed.", e);
@@ -1465,11 +1464,11 @@ class BluetoothManagerService {
        }
        }
    }
    }


    String getAddress(AttributionSource source) {
    String getAddress() {
        mAdapterLock.readLock().lock();
        mAdapterLock.readLock().lock();
        try {
        try {
            if (mAdapter != null) {
            if (mAdapter != null) {
                return mAdapter.getAddress(source);
                return mAdapter.getAddress(mContext.getAttributionSource());
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(
            Log.e(
@@ -1486,11 +1485,11 @@ class BluetoothManagerService {
        return mAddress;
        return mAddress;
    }
    }


    String getName(AttributionSource source) {
    String getName() {
        mAdapterLock.readLock().lock();
        mAdapterLock.readLock().lock();
        try {
        try {
            if (mAdapter != null) {
            if (mAdapter != null) {
                return mAdapter.getName(source);
                return mAdapter.getName(mContext.getAttributionSource());
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
            Log.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
@@ -2193,7 +2192,7 @@ class BluetoothManagerService {
        if (prevBrEdrState != newBrEdrState) { // Only broadcast when there is a BrEdr state change.
        if (prevBrEdrState != newBrEdrState) { // Only broadcast when there is a BrEdr state change.
            if (newBrEdrState == STATE_OFF) {
            if (newBrEdrState == STATE_OFF) {
                sendBluetoothOffCallback();
                sendBluetoothOffCallback();
                sendBrEdrDownCallback(mContext.getAttributionSource());
                sendBrEdrDownCallback();
            }
            }
            broadcastIntentStateChange(
            broadcastIntentStateChange(
                    BluetoothAdapter.ACTION_STATE_CHANGED, prevBrEdrState, newBrEdrState);
                    BluetoothAdapter.ACTION_STATE_CHANGED, prevBrEdrState, newBrEdrState);
+9 −9
Original line number Original line Diff line number Diff line
@@ -127,7 +127,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
            return false;
            return false;
        }
        }


        if (!mPermissionUtils.isCallerNfc(getCallingAppId())) {
        if (!BtPermissionUtils.isCallerNfc(getCallingAppId())) {
            throw new SecurityException("No permission to enable Bluetooth quietly");
            throw new SecurityException("No permission to enable Bluetooth quietly");
        }
        }


@@ -139,7 +139,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
        requireNonNull(source, "AttributionSource cannot be null in disable");
        requireNonNull(source, "AttributionSource cannot be null in disable");


        if (!persist) {
        if (!persist) {
            mPermissionUtils.enforcePrivileged(mContext);
            BtPermissionUtils.enforcePrivileged(mContext);
        }
        }


        final String errorMsg =
        final String errorMsg =
@@ -192,7 +192,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
            return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
            return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
        }
        }


        return mBluetoothManagerService.getAddress(source);
        return mBluetoothManagerService.getAddress();
    }
    }


    @Override
    @Override
@@ -211,7 +211,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
            return null;
            return null;
        }
        }


        return mBluetoothManagerService.getName(source);
        return mBluetoothManagerService.getName();
    }
    }


    @Override
    @Override
@@ -219,14 +219,14 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
    public boolean onFactoryReset(AttributionSource source) {
    public boolean onFactoryReset(AttributionSource source) {
        requireNonNull(source, "AttributionSource cannot be null in onFactoryReset");
        requireNonNull(source, "AttributionSource cannot be null in onFactoryReset");


        mPermissionUtils.enforcePrivileged(mContext);
        BtPermissionUtils.enforcePrivileged(mContext);


        if (!checkConnectPermissionForDataDelivery(
        if (!checkConnectPermissionForDataDelivery(
                mContext, mPermissionManager, source, "onFactoryReset")) {
                mContext, mPermissionManager, source, "onFactoryReset")) {
            return false;
            return false;
        }
        }


        return mBluetoothManagerService.onFactoryReset(source);
        return mBluetoothManagerService.onFactoryReset();
    }
    }


    @Override
    @Override
@@ -277,7 +277,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
            return false;
            return false;
        }
        }


        return mBluetoothManagerService.disableBle(source, source.getPackageName(), token);
        return mBluetoothManagerService.disableBle(source.getPackageName(), token);
    }
    }


    @Override
    @Override
@@ -293,7 +293,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
    @Override
    @Override
    @RequiresPermission(BLUETOOTH_PRIVILEGED)
    @RequiresPermission(BLUETOOTH_PRIVILEGED)
    public int setBtHciSnoopLogMode(int mode) {
    public int setBtHciSnoopLogMode(int mode) {
        mPermissionUtils.enforcePrivileged(mContext);
        BtPermissionUtils.enforcePrivileged(mContext);


        return mBluetoothManagerService.setBtHciSnoopLogMode(mode);
        return mBluetoothManagerService.setBtHciSnoopLogMode(mode);
    }
    }
@@ -301,7 +301,7 @@ class BluetoothServiceBinder extends IBluetoothManager.Stub {
    @Override
    @Override
    @RequiresPermission(BLUETOOTH_PRIVILEGED)
    @RequiresPermission(BLUETOOTH_PRIVILEGED)
    public int getBtHciSnoopLogMode() {
    public int getBtHciSnoopLogMode() {
        mPermissionUtils.enforcePrivileged(mContext);
        BtPermissionUtils.enforcePrivileged(mContext);


        return mBluetoothManagerService.getBtHciSnoopLogMode();
        return mBluetoothManagerService.getBtHciSnoopLogMode();
    }
    }
+7 −7
Original line number Original line Diff line number Diff line
@@ -248,10 +248,10 @@ public class BluetoothServiceBinderTest {
        // TODO(b/280518177): add more test from not System / ...
        // TODO(b/280518177): add more test from not System / ...
        // TODO(b/280518177): add more test when caller is not in foreground
        // TODO(b/280518177): add more test when caller is not in foreground


        doReturn("foo").when(mManagerService).getAddress(any());
        doReturn("foo").when(mManagerService).getAddress();
        assertThat(mBinder.getAddress(mSource)).isEqualTo("foo");
        assertThat(mBinder.getAddress(mSource)).isEqualTo("foo");


        verify(mManagerService).getAddress(any());
        verify(mManagerService).getAddress();
        verifyMockForCheckIfCallerIsForegroundUser();
        verifyMockForCheckIfCallerIsForegroundUser();
    }
    }


@@ -267,9 +267,9 @@ public class BluetoothServiceBinderTest {
        // TODO(b/280518177): add more test from not System / ...
        // TODO(b/280518177): add more test from not System / ...
        // TODO(b/280518177): add more test when caller is not in foreground
        // TODO(b/280518177): add more test when caller is not in foreground


        doReturn("foo").when(mManagerService).getName(any());
        doReturn("foo").when(mManagerService).getName();
        assertThat(mBinder.getName(mSource)).isEqualTo("foo");
        assertThat(mBinder.getName(mSource)).isEqualTo("foo");
        verify(mManagerService).getName(any());
        verify(mManagerService).getName();
        verifyMockForCheckIfCallerIsForegroundUser();
        verifyMockForCheckIfCallerIsForegroundUser();
    }
    }


@@ -288,7 +288,7 @@ public class BluetoothServiceBinderTest {
                .adoptShellPermissionIdentity(BLUETOOTH_PRIVILEGED, BLUETOOTH_CONNECT);
                .adoptShellPermissionIdentity(BLUETOOTH_PRIVILEGED, BLUETOOTH_CONNECT);


        assertThat(mBinder.onFactoryReset(mSource)).isFalse();
        assertThat(mBinder.onFactoryReset(mSource)).isFalse();
        verify(mManagerService).onFactoryReset(any());
        verify(mManagerService).onFactoryReset();
        verifyMock();
        verifyMock();
    }
    }


@@ -322,9 +322,9 @@ public class BluetoothServiceBinderTest {


        checkDisabled(() -> mBinder.disableBle(mSource, token));
        checkDisabled(() -> mBinder.disableBle(mSource, token));
        checkHardDenied(() -> mBinder.disableBle(mSource, token), false);
        checkHardDenied(() -> mBinder.disableBle(mSource, token), false);
        doReturn(true).when(mManagerService).disableBle(eq(mSource), eq(TAG), eq(token));
        doReturn(true).when(mManagerService).disableBle(eq(TAG), eq(token));
        checkGranted(() -> mBinder.disableBle(mSource, token), true);
        checkGranted(() -> mBinder.disableBle(mSource, token), true);
        verify(mManagerService).disableBle(eq(mSource), eq(TAG), eq(token));
        verify(mManagerService).disableBle(eq(TAG), eq(token));
        verifyMock();
        verifyMock();
    }
    }