From 14c1c7b4a732c517ba18f5dd0598adb9f3b72221 Mon Sep 17 00:00:00 2001 From: Bernardo Rufino Date: Wed, 26 May 2021 16:25:39 +0100 Subject: [PATCH] Fix background bypass via notifications This is a CP of ag/14736230 to qt-dev. Apps were able to bypass BAL and BG-FGS restrictions by retrieving their own notifications and firing their PI since those were allowlisted for those operations. Now we strip the token that granted them that ability from notifications returned via NM.getActiveNotifications(), which returns the notifications of the caller. Notifications returned via notification listener APIs still contain such token, as they should. Bug: 185388103 Bug: 169821287 Test: Manually tested Change-Id: I2ede0d639a560f6acacec3864a0a7d23af152ba5 Merged-In: I2ede0d639a560f6acacec3864a0a7d23af152ba5 (cherry picked from commit 5fbeff59df3ea1441c3843aa1834616876ef1985) --- core/java/android/app/Notification.java | 13 +++++++++++++ .../notification/NotificationManagerService.java | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index c266fbf8be0bc..c3147de96128e 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -2950,6 +2950,19 @@ public class Notification implements Parcelable builder.build(); // callers expect this notification to be ready to use } + /** + * Sets the token used for background operations for the pending intents associated with this + * notification. + * + * This token is automatically set during deserialization for you, you usually won't need to + * call this unless you want to change the existing token, if any. + * + * @hide + */ + public void setAllowlistToken(@Nullable IBinder token) { + mWhitelistToken = token; + } + /** * @hide */ diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 6531504b071fe..7c6c2999325e9 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3058,6 +3058,7 @@ public class NotificationManagerService extends SystemService { } } + /** Notifications returned here will have allowlistToken stripped from them. */ private StatusBarNotification sanitizeSbn(String pkg, int userId, StatusBarNotification sbn) { if (sbn.getUserId() == userId) { @@ -3065,11 +3066,16 @@ public class NotificationManagerService extends SystemService { // We could pass back a cloneLight() but clients might get confused and // try to send this thing back to notify() again, which would not work // very well. + Notification notification = sbn.getNotification().clone(); + // Remove background token before returning notification to untrusted app, this + // ensures the app isn't able to perform background operations that are + // associated with notification interactions. + notification.setAllowlistToken(null); return new StatusBarNotification( sbn.getPackageName(), sbn.getOpPkg(), sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), - sbn.getNotification().clone(), + notification, sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); } }