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

Commit 2450db33 authored by Daniel Kim's avatar Daniel Kim Committed by Android (Google) Code Review
Browse files

Merge "Verify caller is cred autofill service" into main

parents 0a40deba 571a97ad
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.credentials.ClearCredentialStateRequest;
import android.credentials.CreateCredentialException;
import android.credentials.CreateCredentialRequest;
@@ -50,6 +51,7 @@ import android.credentials.ISetEnabledProvidersCallback;
import android.credentials.PrepareGetCredentialResponseInternal;
import android.credentials.RegisterCredentialDescriptionRequest;
import android.credentials.UnregisterCredentialDescriptionRequest;
import android.credentials.flags.Flags;
import android.os.Binder;
import android.os.CancellationSignal;
import android.os.IBinder;
@@ -79,6 +81,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -535,6 +538,33 @@ public final class CredentialManagerService

            final int userId = UserHandle.getCallingUserId();
            final int callingUid = Binder.getCallingUid();
            if (Flags.safeguardCandidateCredentialsApiCaller()) {
                try {
                    String credentialManagerAutofillCompName = mContext.getResources().getString(
                            R.string.config_defaultCredentialManagerAutofillService);
                    ComponentName componentName = ComponentName.unflattenFromString(
                            credentialManagerAutofillCompName);
                    if (componentName == null) {
                        throw new SecurityException(
                                "Credential Autofill service does not exist on this device.");
                    }
                    PackageManager pm = mContext.createContextAsUser(
                            UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
                    String callingProcessPackage = pm.getNameForUid(callingUid);
                    if (callingProcessPackage == null) {
                        throw new SecurityException(
                                "Couldn't determine the identity of the caller.");
                    }
                    if (!Objects.equals(componentName.getPackageName(), callingProcessPackage)) {
                        throw new SecurityException(callingProcessPackage
                                + " is not the device's credential autofill package.");
                    }
                } catch (Resources.NotFoundException e) {
                    throw new SecurityException(
                            "Credential Autofill service does not exist on this device.");
                }
            }


            // New request session, scoped for this request only.
            final GetCandidateRequestSession session =