From c9c3853e59462d3a15e01be877d03c4285139f1d Mon Sep 17 00:00:00 2001 From: chiachangwang Date: Thu, 11 May 2023 15:00:31 +0000 Subject: [PATCH] Clear caller identity in IPC methods A utility method should not be responsible for clearing the caller identity. Clearing caller identity should happen in methods that process binder IPCs, not general utility methods. Update it to clear in each method. Bug: 279381469 Test: atest FrameworksNetTests Change-Id: I95b520fb0d7851ec3e14fbd16044d5f877c74561 --- .../AutomaticOnOffKeepaliveTracker.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java index e2ef981113..f8285ed711 100644 --- a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java +++ b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java @@ -210,8 +210,12 @@ public class AutomaticOnOffKeepaliveTracker { this.mKi = Objects.requireNonNull(ki); mCallback = ki.mCallback; mUnderpinnedNetwork = underpinnedNetwork; - if (autoOnOff && mDependencies.isFeatureEnabled(AUTOMATIC_ON_OFF_KEEPALIVE_VERSION, - true /* defaultEnabled */)) { + // Reading DeviceConfig will check if the calling uid and calling package name are the + // same. Clear calling identity to align the calling uid and package + final boolean enabled = BinderUtils.withCleanCallingIdentity( + () -> mDependencies.isFeatureEnabled(AUTOMATIC_ON_OFF_KEEPALIVE_VERSION, + true /* defaultEnabled */)); + if (autoOnOff && enabled) { mAutomaticOnOffState = STATE_ENABLED; if (null == ki.mFd) { throw new IllegalArgumentException("fd can't be null with automatic " @@ -583,8 +587,12 @@ public class AutomaticOnOffKeepaliveTracker { */ public void dump(IndentingPrintWriter pw) { mKeepaliveTracker.dump(pw); - final boolean featureEnabled = mDependencies.isFeatureEnabled( - AUTOMATIC_ON_OFF_KEEPALIVE_VERSION, true /* defaultEnabled */); + // Reading DeviceConfig will check if the calling uid and calling package name are the same. + // Clear calling identity to align the calling uid and package so that it won't fail if cts + // would like to call dump() + final boolean featureEnabled = BinderUtils.withCleanCallingIdentity( + () -> mDependencies.isFeatureEnabled(AUTOMATIC_ON_OFF_KEEPALIVE_VERSION, + true /* defaultEnabled */)); pw.println("AutomaticOnOff enabled: " + featureEnabled); pw.increaseIndent(); for (AutomaticOnOffKeepalive autoKi : mAutomaticOnOffKeepalives) { @@ -837,12 +845,8 @@ public class AutomaticOnOffKeepaliveTracker { * @return whether the feature is enabled */ public boolean isFeatureEnabled(@NonNull final String name, final boolean defaultEnabled) { - // Reading DeviceConfig will check if the calling uid and calling package name are the - // same. Clear calling identity to align the calling uid and package so that it won't - // fail if cts would like to do the dump() - return BinderUtils.withCleanCallingIdentity(() -> - DeviceConfigUtils.isFeatureEnabled(mContext, NAMESPACE_TETHERING, name, - DeviceConfigUtils.TETHERING_MODULE_NAME, defaultEnabled)); + return DeviceConfigUtils.isFeatureEnabled(mContext, NAMESPACE_TETHERING, name, + DeviceConfigUtils.TETHERING_MODULE_NAME, defaultEnabled); } /**