Fix calling package detection in KK+

This commit is contained in:
Marvin W 2019-06-01 15:57:46 +01:00
parent a74ce16d2a
commit 4713797edc
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
2 changed files with 36 additions and 5 deletions

View File

@ -30,8 +30,6 @@ import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.gms.R;
import org.microg.gms.common.PackageUtils;
import java.util.Arrays;
@ -52,7 +50,12 @@ public class AccountContentProvider extends ContentProvider {
@Nullable
@Override
public Bundle call(String method, String arg, Bundle extras) {
String packageName = PackageUtils.packageFromProcessId(getContext(), Binder.getCallingPid());
String suggestedPackageName = null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
suggestedPackageName = getCallingPackage();
}
String packageName = PackageUtils.getAndCheckCallingPackage(getContext(), suggestedPackageName);
Log.d(TAG, "Call from " + packageName);
if (!PackageUtils.callerHasExtendedAccess(getContext())) {
String[] packagesForUid = getContext().getPackageManager().getPackagesForUid(Binder.getCallingUid());
if (packagesForUid != null && packagesForUid.length != 0)
@ -64,7 +67,14 @@ public class AccountContentProvider extends ContentProvider {
if (PROVIDER_METHOD_GET_ACCOUNTS.equals(method) && AuthConstants.DEFAULT_ACCOUNT_TYPE.equals(arg)) {
Bundle result = new Bundle();
AccountManager am = AccountManager.get(getContext());
result.putParcelableArray(PROVIDER_EXTRA_ACCOUNTS, Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? am.getAccountsByTypeForPackage(arg, packageName) : am.getAccountsByType(arg));
Account[] accounts = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
accounts = am.getAccountsByTypeForPackage(arg, packageName);
}
if (accounts == null || accounts.length == 0) {
accounts = am.getAccountsByType(arg);
}
result.putParcelableArray(PROVIDER_EXTRA_ACCOUNTS, accounts);
return result;
} else if (PROVIDER_METHOD_CLEAR_PASSWORD.equals(method)) {
Account a = extras.getParcelable(PROVIDER_EXTRA_CLEAR_PASSWORD);

View File

@ -110,6 +110,16 @@ public class PackageUtils {
return null;
}
@Nullable
public static String getCallingPackage(Context context) {
int callingUid = Binder.getCallingUid(), callingPid = Binder.getCallingPid();
String packageName = packageFromProcessId(context, callingPid);
if (packageName == null) {
packageName = firstPackageFromUserId(context, callingUid);
}
return packageName;
}
@Nullable
public static String getAndCheckCallingPackage(Context context, String suggestedPackageName) {
return getAndCheckCallingPackage(context, suggestedPackageName, 0);
@ -152,10 +162,12 @@ public class PackageUtils {
packageName = packagesForUid[0];
} else if (Arrays.asList(packagesForUid).contains(suggestedPackageName)) {
packageName = suggestedPackageName;
} else if (suggestedPackageName == null) {
packageName = packagesForUid[0];
}
}
}
if (packageName != null && !packageName.equals(suggestedPackageName)) {
if (packageName != null && suggestedPackageName != null && !packageName.equals(suggestedPackageName)) {
throw new SecurityException("UID [" + callingUid + "] is not related to packageName [" + packageName + "]");
}
return packageName;
@ -172,6 +184,15 @@ public class PackageUtils {
return null;
}
@Nullable
public static String firstPackageFromUserId(Context context, int uid) {
String[] packagesForUid = context.getPackageManager().getPackagesForUid(uid);
if (packagesForUid != null && packagesForUid.length != 0) {
return packagesForUid[0];
}
return null;
}
@SuppressWarnings("deprecation")
public static String packageFromPendingIntent(PendingIntent pi) {
if (pi == null) return null;