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

Commit a57b3bd0 authored by Jack Wang's avatar Jack Wang
Browse files

Grant client app visibility with UID from activity record

Bug: 226167520
Test: atest android.voiceinteraction.cts
Change-Id: Ibe20fb4da91fa80f8539bb19c90515167bf6e85b
parent 5efaa27f
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -338,14 +338,16 @@ public abstract class ActivityTaskManagerInternal {
        private final @NonNull IBinder mAssistToken;
        private final @NonNull IBinder mShareableActivityToken;
        private final @NonNull IApplicationThread mAppThread;
        private final int mUid;

        public ActivityTokens(@NonNull IBinder activityToken,
                @NonNull IBinder assistToken, @NonNull IApplicationThread appThread,
                @NonNull IBinder shareableActivityToken) {
                @NonNull IBinder shareableActivityToken, int uid) {
            mActivityToken = activityToken;
            mAssistToken = assistToken;
            mAppThread = appThread;
            mShareableActivityToken = shareableActivityToken;
            mUid = uid;
        }

        /**
@@ -375,6 +377,13 @@ public abstract class ActivityTaskManagerInternal {
        public @NonNull IApplicationThread getApplicationThread() {
            return mAppThread;
        }

        /**
         * @return The UID of the activity
         */
        public int getUid() {
            return mUid;
        }
    }

    public abstract void sendActivityResult(int callingUid, IBinder activityToken,
+4 −2
Original line number Diff line number Diff line
@@ -5892,14 +5892,16 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
                if (token == null && list.get(0).attachedToProcess()) {
                    ActivityRecord topRecord = list.get(0);
                    return new ActivityTokens(topRecord.token, topRecord.assistToken,
                            topRecord.app.getThread(), topRecord.shareableActivityToken);
                            topRecord.app.getThread(), topRecord.shareableActivityToken,
                            topRecord.getUid());
                }
                // find the expected Activity
                for (int i = 0; i < list.size(); i++) {
                    ActivityRecord record = list.get(i);
                    if (record.shareableActivityToken == token && record.attachedToProcess()) {
                        return new ActivityTokens(record.token, record.assistToken,
                                record.app.getThread(), record.shareableActivityToken);
                                record.app.getThread(), record.shareableActivityToken,
                                record.getUid());
                    }
                }
                return null;
+5 −33
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.ShortcutServiceInternal;
@@ -105,7 +104,6 @@ import com.android.server.pm.permission.LegacyPermissionManagerInternal;
import com.android.server.soundtrigger.SoundTriggerInternal;
import com.android.server.utils.TimingsTraceAndSlog;
import com.android.server.wm.ActivityTaskManagerInternal;
import com.android.server.wm.ActivityTaskManagerInternal.ActivityTokens;

import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -128,7 +126,6 @@ public class VoiceInteractionManagerService extends SystemService {
    final ActivityManagerInternal mAmInternal;
    final ActivityTaskManagerInternal mAtmInternal;
    final UserManagerInternal mUserManagerInternal;
    final PackageManagerInternal mPackageManagerInternal;
    final ArrayMap<Integer, VoiceInteractionManagerServiceStub.SoundTriggerSession>
            mLoadedKeyphraseIds = new ArrayMap<>();
    ShortcutServiceInternal mShortcutServiceInternal;
@@ -149,8 +146,6 @@ public class VoiceInteractionManagerService extends SystemService {
                LocalServices.getService(ActivityTaskManagerInternal.class));
        mUserManagerInternal = Objects.requireNonNull(
                LocalServices.getService(UserManagerInternal.class));
        mPackageManagerInternal = Objects.requireNonNull(
                LocalServices.getService(PackageManagerInternal.class));

        LegacyPermissionManagerInternal permissionManagerInternal = LocalServices.getService(
                LegacyPermissionManagerInternal.class);
@@ -374,21 +369,6 @@ public class VoiceInteractionManagerService extends SystemService {
            return new SoundTriggerSessionBinderProxy(session);
        }

        @GuardedBy("this")
        private void grantImplicitAccessLocked(int grantRecipientUid, @Nullable Intent intent) {
            if (mImpl == null) {
                Slog.w(TAG, "Cannot grant implicit access because mImpl is null.");
                return;
            }

            final int grantRecipientAppId = UserHandle.getAppId(grantRecipientUid);
            final int grantRecipientUserId = UserHandle.getUserId(grantRecipientUid);
            final int voiceInteractionUid = mImpl.mInfo.getServiceInfo().applicationInfo.uid;
            mPackageManagerInternal.grantImplicitAccess(
                    grantRecipientUserId, intent, grantRecipientAppId, voiceInteractionUid,
                    /* direct= */ true);
        }

        private IVoiceInteractionSoundTriggerSession createSoundTriggerSessionForSelfIdentity(
                IBinder client) {
            Identity identity = new Identity();
@@ -419,10 +399,11 @@ public class VoiceInteractionManagerService extends SystemService {
                            @Override
                            public void onShown() {
                                synchronized (VoiceInteractionManagerServiceStub.this) {
                                    VoiceInteractionManagerServiceStub.this
                                            .grantImplicitAccessLocked(callingUid,
                                    if (mImpl != null) {
                                        mImpl.grantImplicitAccessLocked(callingUid,
                                                /* intent= */ null);
                                    }
                                }
                                mAtmInternal.onLocalVoiceInteractionStarted(token,
                                        mImpl.mActiveSession.mSession,
                                        mImpl.mActiveSession.mInteractor);
@@ -995,7 +976,7 @@ public class VoiceInteractionManagerService extends SystemService {
                            mContext.getPackageManager(), PackageManager.MATCH_ALL);
                    if (activityInfo != null) {
                        final int activityUid = activityInfo.applicationInfo.uid;
                        grantImplicitAccessLocked(activityUid, intent);
                        mImpl.grantImplicitAccessLocked(activityUid, intent);
                    } else {
                        Slog.w(TAG, "Cannot find ActivityInfo in startVoiceActivity.");
                    }
@@ -1039,15 +1020,6 @@ public class VoiceInteractionManagerService extends SystemService {
                }
                final long caller = Binder.clearCallingIdentity();
                try {
                    // Getting the UID corresponding to the taskId, and grant the visibility to it.
                    final ActivityTokens tokens = mAtmInternal
                            .getAttachedNonFinishingActivityForTask(taskId, /* token= */ null);
                    final ComponentName componentName = mAtmInternal.getActivityName(
                            tokens.getActivityToken());
                    grantImplicitAccessLocked(mPackageManagerInternal.getPackageUid(
                            componentName.getPackageName(), PackageManager.MATCH_ALL,
                                    UserHandle.myUserId()), /* intent= */ null);

                    mImpl.requestDirectActionsLocked(token, taskId, assistToken,
                            cancellationCallback, resultCallback);
                } finally {
+15 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ParceledListSlice;
import android.content.pm.ServiceInfo;
import android.hardware.soundtrigger.IRecognitionStatusCallback;
@@ -79,6 +80,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConnection.Callback {
    final static String TAG = "VoiceInteractionServiceManager";
@@ -95,6 +97,7 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
    final ComponentName mComponent;
    final IActivityManager mAm;
    final IActivityTaskManager mAtm;
    final PackageManagerInternal mPackageManagerInternal;
    final VoiceInteractionServiceInfo mInfo;
    final ComponentName mSessionComponentName;
    final IWindowManager mIWindowManager;
@@ -189,6 +192,8 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
        mComponent = service;
        mAm = ActivityManager.getService();
        mAtm = ActivityTaskManager.getService();
        mPackageManagerInternal = Objects.requireNonNull(
                LocalServices.getService(PackageManagerInternal.class));
        VoiceInteractionServiceInfo info;
        try {
            info = new VoiceInteractionServiceInfo(context.getPackageManager(), service, mUser);
@@ -224,6 +229,15 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
                Context.RECEIVER_EXPORTED);
    }

    public void grantImplicitAccessLocked(int grantRecipientUid, @Nullable Intent intent) {
        final int grantRecipientAppId = UserHandle.getAppId(grantRecipientUid);
        final int grantRecipientUserId = UserHandle.getUserId(grantRecipientUid);
        final int voiceInteractionUid = mInfo.getServiceInfo().applicationInfo.uid;
        mPackageManagerInternal.grantImplicitAccess(
                grantRecipientUserId, intent, grantRecipientAppId, voiceInteractionUid,
                /* direct= */ true);
    }

    public boolean showSessionLocked(Bundle args, int flags,
            IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
        if (mActiveSession == null) {
@@ -345,6 +359,7 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
            Slog.w(TAG, "Unknown activity to query for direct actions");
            callback.sendResult(null);
        } else {
            grantImplicitAccessLocked(tokens.getUid(), /* intent= */ null);
            try {
                tokens.getApplicationThread().requestDirectActions(tokens.getActivityToken(),
                        mActiveSession.mInteractor, cancellationCallback, callback);