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

Commit 25b202f2 authored by Ayush Sharma's avatar Ayush Sharma Committed by Automerger Merge Worker
Browse files

Merge "Add check to verify package belongs to caller" into sc-qpr1-dev am: 4dd98007

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15777416

Change-Id: I343091c370f49ee48774a7f9c7d9ca63ab784177
parents d6150497 4dd98007
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -19,10 +19,13 @@ package com.android.server.os;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.os.IDeviceIdentifiersPolicyService;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.UserHandle;

import com.android.internal.telephony.TelephonyPermissions;
import com.android.server.SystemService;
@@ -65,11 +68,31 @@ public final class DeviceIdentifiersPolicyService extends SystemService {
        @Override
        public @Nullable String getSerialForPackage(String callingPackage,
                String callingFeatureId) throws RemoteException {
            if (!checkPackageBelongsToCaller(callingPackage)) {
                throw new IllegalArgumentException(
                        "Invalid callingPackage or callingPackage does not belong to caller's uid:"
                                + Binder.getCallingUid());
            }

            if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
                    callingPackage, callingFeatureId, "getSerial")) {
                return Build.UNKNOWN;
            }
            return SystemProperties.get("ro.serialno", Build.UNKNOWN);
        }

        private boolean checkPackageBelongsToCaller(String callingPackage) {
            int callingUid = Binder.getCallingUid();
            int callingUserId = UserHandle.getUserId(callingUid);
            int callingPackageUid;
            try {
                callingPackageUid = mContext.getPackageManager().getPackageUidAsUser(
                        callingPackage, callingUserId);
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }

            return callingPackageUid == callingUid;
        }
    }
}