Do not throw SecurityException from getPrimaryClip.

Currently getPrimaryClip throws a SecurityException if a Uri in
clipboard has become invalid and permission to it can no longer be
granted. This is unexpected and can crash apps that are trying to
paste.

This change catches the exception and clears clipboard if this case
occurs.

Bug: 187942504
Test: atest android.appsecurity.cts.AppSecurityTests#testPermissionDiffCert
Test: build & flash, create invalid Uri by uninstalling and reinstalling Chrome
Change-Id: I9994bb03f1aed47e32a500bba8886eea3f2cd905
This commit is contained in:
Oli Lan
2021-05-12 17:38:19 +01:00
parent d261ad7bfe
commit 6bc896f705

View File

@@ -379,7 +379,15 @@ public class ClipboardService extends SystemService {
return null;
}
synchronized (mLock) {
addActiveOwnerLocked(intendingUid, pkg);
try {
addActiveOwnerLocked(intendingUid, pkg);
} catch (SecurityException e) {
// Permission could not be granted - URI may be invalid
Slog.i(TAG, "Could not grant permission to primary clip. Clearing clipboard.");
setPrimaryClipInternalLocked(null, intendingUid, pkg);
return null;
}
PerUserClipboard clipboard = getClipboardLocked(intendingUserId);
showAccessNotificationLocked(pkg, intendingUid, intendingUserId, clipboard);
notifyTextClassifierLocked(clipboard, pkg, intendingUid);