Skip to content

Commit ae47ae9

Browse files
committed
Fix SecurityException crash in work profiles for getPackagesForUid
In work profiles (e.g. Shelter), getPackagesForUid() throws SecurityException for cross-user UIDs because the app lacks INTERACT_ACROSS_USERS_FULL permission. This was already known and handled in AdapterLog.java but NOT in the critical packet processing path. The unhandled exception in shouldTrackApp() propagates up through blockKnownTracker() and log(), causing BOTH tracker blocking AND traffic logging to silently fail for every packet - the exception is caught by the outer handleMessage() try-catch and swallowed. Fix: wrap getPackagesForUid() in try-catch SecurityException at all three unprotected call sites in ServiceSinkhole: - shouldTrackApp() (blocking + logging pipeline) - default to tracking - prepareUidIPFilters() (lockdown check) - skip lockdown filter - showNewInstallNotification() - skip notification gracefully This is very likely the root cause of the reported work profile issue where "tracker blocking and traffic log doesn't work in Shelter." https://claude.ai/code/session_013NayXYhZViADqMfUXT4VuF
1 parent 4583b66 commit ae47ae9

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,13 @@ private void prepareUidIPFilters(String dname) {
18791879
long ttl = (cursor.isNull(colTTL) ? 7 * 24 * 3600 * 1000L : cursor.getLong(colTTL));
18801880

18811881
if (isLockedDown(last_metered)) {
1882-
String[] pkg = getPackageManager().getPackagesForUid(uid);
1882+
String[] pkg;
1883+
try {
1884+
pkg = getPackageManager().getPackagesForUid(uid);
1885+
} catch (SecurityException ignored) {
1886+
// Work profile cross-user UID
1887+
pkg = null;
1888+
}
18831889
if (pkg != null && pkg.length > 0) {
18841890
if (!lockdown.getBoolean(pkg[0], false))
18851891
continue;
@@ -2230,7 +2236,16 @@ private Allowed isAddressAllowed(Packet packet) {
22302236
* - It's a system app and manage_system is disabled
22312237
*/
22322238
private boolean shouldTrackApp(int uid) {
2233-
String[] packages = getPackageManager().getPackagesForUid(uid);
2239+
String[] packages;
2240+
try {
2241+
packages = getPackageManager().getPackagesForUid(uid);
2242+
} catch (SecurityException ex) {
2243+
// In work profiles, getPackagesForUid throws SecurityException for
2244+
// cross-user UIDs (requires INTERACT_ACROSS_USERS_FULL permission).
2245+
// Default to tracking so blocking/logging still works.
2246+
Log.w(TAG, "SecurityException in shouldTrackApp for uid " + uid + ": " + ex.getMessage());
2247+
return true;
2248+
}
22342249
if (packages == null || packages.length == 0) {
22352250
return true; // Unknown UID, default to tracking
22362251
}
@@ -2743,6 +2758,9 @@ public void notifyNewApplication(int uid, BroadcastReceiver br) {
27432758

27442759
} catch (PackageManager.NameNotFoundException ex) {
27452760
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
2761+
} catch (SecurityException ex) {
2762+
// Work profile cross-user UID
2763+
Log.w(TAG, "SecurityException showing install notification for uid " + uid + ": " + ex.getMessage());
27462764
}
27472765
}
27482766

0 commit comments

Comments
 (0)