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

Commit 5c887619 authored by Andrea Ambu's avatar Andrea Ambu
Browse files

speech: Check componentName maps to RecognitionService

Bug: 188908756
Fix: 188908756
Test: CtsVoiceRecognitionTestCases and manual on bug POC
Change-Id: Ic2395f62499367cf882340598dde6e79e4723a61
parent 8d708674
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.content.AttributionSource;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Binder;
import android.os.IBinder;
@@ -31,6 +32,7 @@ import android.os.RemoteException;
import android.speech.IRecognitionListener;
import android.speech.IRecognitionService;
import android.speech.IRecognitionServiceManagerCallback;
import android.speech.RecognitionService;
import android.speech.SpeechRecognizer;
import android.util.Slog;

@@ -39,6 +41,7 @@ import com.android.server.infra.AbstractPerUserSystemService;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -225,6 +228,10 @@ final class SpeechRecognitionManagerServiceImpl extends
                }
            }

            if (serviceComponent != null && !componentMapsToRecognitionService(serviceComponent)) {
                return null;
            }

            RemoteSpeechRecognitionService service =
                    new RemoteSpeechRecognitionService(
                            getContext(), serviceComponent, getUserId(), callingUid);
@@ -241,6 +248,25 @@ final class SpeechRecognitionManagerServiceImpl extends
        }
    }

    private boolean componentMapsToRecognitionService(@NonNull ComponentName serviceComponent) {
        List<ResolveInfo> resolveInfos =
                getContext().getPackageManager().queryIntentServices(
                        new Intent(RecognitionService.SERVICE_INTERFACE), 0);
        if (resolveInfos == null) {
            return false;
        }

        for (ResolveInfo ri : resolveInfos) {
            if (ri.serviceInfo != null
                    && serviceComponent.equals(ri.serviceInfo.getComponentName())) {
                return true;
            }
        }

        Slog.w(TAG, "serviceComponent is not RecognitionService: " + serviceComponent);
        return false;
    }

    private void removeService(int callingUid, RemoteSpeechRecognitionService service) {
        synchronized (mLock) {
            Set<RemoteSpeechRecognitionService> valuesByCaller =