diff options
author | Felipe Leme <felipeal@google.com> | 2019-03-26 14:02:25 -0700 |
---|---|---|
committer | Felipe Leme <felipeal@google.com> | 2019-03-28 19:31:52 -0700 |
commit | afbba9fb3681515288608402390904f1cb96546b (patch) | |
tree | b732fb3d1ebbfdbc00bbbd56e6c0530d662edef2 /services/contentcapture | |
parent | a8d33c24f8531b65020436c15d868d72677a5b97 (diff) |
Checks package name belongs to called UID on some content capture methods.
Also refactored how the SecurityException is thrown back to the caller app.
Bug: 122959591
Test: manual verification
Test: atest CtsContentCaptureServiceTestCases # sanity check (minus usual flakiness)
Change-Id: I4d2a68e61dc1c801d80734a30f4bbe6fdae8555d
Diffstat (limited to 'services/contentcapture')
-rw-r--r-- | services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java b/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java index 2894662becac..9b02c4e72cfc 100644 --- a/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java +++ b/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java @@ -450,23 +450,16 @@ public final class ContentCaptureManagerService extends } @GuardedBy("mLock") - private boolean assertCalledByServiceLocked(@NonNull String methodName, @UserIdInt int userId, - int callingUid, @NonNull IResultReceiver result) { - final boolean isService = isCalledByServiceLocked(methodName, userId, callingUid); - if (isService) return true; - - try { - result.send(RESULT_CODE_SECURITY_EXCEPTION, /* resultData= */ null); - } catch (RemoteException e) { - Slog.w(mTag, "Unable to send isContentCaptureFeatureEnabled(): " + e); + private void assertCalledByServiceLocked(@NonNull String methodName) { + if (!isCalledByServiceLocked(methodName)) { + throw new SecurityException("caller is not user's ContentCapture service"); } - return false; } @GuardedBy("mLock") - private boolean isCalledByServiceLocked(@NonNull String methodName, @UserIdInt int userId, - int callingUid) { - + private boolean isCalledByServiceLocked(@NonNull String methodName) { + final int userId = UserHandle.getCallingUserId(); + final int callingUid = Binder.getCallingUid(); final String serviceName = mServiceNameResolver.getServiceName(userId); if (serviceName == null) { Slog.e(mTag, methodName + ": called by UID " + callingUid @@ -499,6 +492,27 @@ public final class ContentCaptureManagerService extends return true; } + /** + * Executes the given {@code runnable} and if it throws a {@link SecurityException}, + * send it back to the receiver. + * + * @return whether the exception was thrown or not. + */ + private boolean throwsSecurityException(@NonNull IResultReceiver result, + @NonNull Runnable runable) { + try { + runable.run(); + return false; + } catch (SecurityException e) { + try { + result.send(RESULT_CODE_SECURITY_EXCEPTION, bundleFor(e.getMessage())); + } catch (RemoteException e2) { + Slog.w(mTag, "Unable to send security exception (" + e + "): ", e2); + } + } + return true; + } + @Override // from AbstractMasterSystemService protected void dumpLocked(String prefix, PrintWriter pw) { super.dumpLocked(prefix, pw); @@ -570,7 +584,7 @@ public final class ContentCaptureManagerService extends @Override public void removeUserData(@NonNull UserDataRemovalRequest request) { Preconditions.checkNotNull(request); - // TODO(b/122959591): check caller uid owns the package name + assertCalledByPackageOwner(request.getPackageName()); final int userId = UserHandle.getCallingUserId(); synchronized (mLock) { @@ -581,13 +595,14 @@ public final class ContentCaptureManagerService extends @Override public void isContentCaptureFeatureEnabled(@NonNull IResultReceiver result) { - final int userId = UserHandle.getCallingUserId(); boolean enabled; synchronized (mLock) { - final boolean isService = assertCalledByServiceLocked( - "isContentCaptureFeatureEnabled()", userId, Binder.getCallingUid(), result); - if (!isService) return; + if (throwsSecurityException(result, + () -> assertCalledByServiceLocked("isContentCaptureFeatureEnabled()"))) { + return; + } + final int userId = UserHandle.getCallingUserId(); enabled = !mDisabledByDeviceConfig && !isDisabledBySettingsLocked(userId); } try { @@ -599,15 +614,8 @@ public final class ContentCaptureManagerService extends @Override public void getServiceSettingsActivity(@NonNull IResultReceiver result) { - try { - enforceCallingPermissionForManagement(); - } catch (SecurityException e) { - try { - result.send(RESULT_CODE_SECURITY_EXCEPTION, bundleFor(e.getMessage())); - } catch (RemoteException e2) { - Slog.w(mTag, "Unable to send getServiceSettingsIntent() exception: " + e2); - return; - } + if (throwsSecurityException(result, () -> enforceCallingPermissionForManagement())) { + return; } final int userId = UserHandle.getCallingUserId(); @@ -627,7 +635,9 @@ public final class ContentCaptureManagerService extends @Override public void getContentCaptureConditions(@NonNull String packageName, @NonNull IResultReceiver result) { - // TODO(b/122959591): check caller uid owns the package name + if (throwsSecurityException(result, () -> assertCalledByPackageOwner(packageName))) { + return; + } final int userId = UserHandle.getCallingUserId(); final ArrayList<ContentCaptureCondition> conditions; |