Merge "Ensure calling package name and uid are matched" into sc-qpr1-dev am: e93efb6f96

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/15618365

Change-Id: I80aa5da33f8c4c5154a721ceec58852b6c2d42f1
This commit is contained in:
TreeHugger Robot
2021-10-01 14:21:16 +00:00
committed by Automerger Merge Worker
2 changed files with 28 additions and 0 deletions

View File

@@ -2366,6 +2366,26 @@ public class ConnectivityService extends IConnectivityManager.Stub
return false;
}
private int getAppUid(final String app, final UserHandle user) {
final PackageManager pm =
mContext.createContextAsUser(user, 0 /* flags */).getPackageManager();
final long token = Binder.clearCallingIdentity();
try {
return pm.getPackageUid(app, 0 /* flags */);
} catch (PackageManager.NameNotFoundException e) {
return -1;
} finally {
Binder.restoreCallingIdentity(token);
}
}
private void verifyCallingUidAndPackage(String packageName, int callingUid) {
final UserHandle user = UserHandle.getUserHandleForUid(callingUid);
if (getAppUid(packageName, user) != callingUid) {
throw new SecurityException(packageName + " does not belong to uid " + callingUid);
}
}
/**
* Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface.
@@ -2381,6 +2401,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (disallowedBecauseSystemCaller()) {
return false;
}
verifyCallingUidAndPackage(callingPackageName, mDeps.getCallingUid());
enforceChangePermission(callingPackageName, callingAttributionTag);
if (mProtectedNetworks.contains(networkType)) {
enforceConnectivityRestrictedNetworksPermission();

View File

@@ -14411,4 +14411,11 @@ public class ConnectivityServiceTest {
mDefaultNetworkCallback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
}
@Test
public void testRequestRouteToHostAddress_PackageDoesNotBelongToCaller() {
assertThrows(SecurityException.class, () -> mService.requestRouteToHostAddress(
ConnectivityManager.TYPE_NONE, null /* hostAddress */, "com.not.package.owner",
null /* callingAttributionTag */));
}
}