Skip to content

Commit

Permalink
Fix calling package detection in KK+
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Jun 1, 2019
1 parent a74ce16 commit 4713797
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
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;
Expand All @@ -52,7 +50,12 @@ public boolean onCreate() {
@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)
Expand All @@ -64,7 +67,14 @@ public Bundle call(String method, String arg, Bundle extras) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public static String firstSignatureDigest(Context context, String packageName) {
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);
Expand Down Expand Up @@ -152,10 +162,12 @@ public static String getAndCheckPackage(Context context, String suggestedPackage
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;
Expand All @@ -172,6 +184,15 @@ public static String packageFromProcessId(Context context, int pid) {
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;
Expand Down

0 comments on commit 4713797

Please sign in to comment.