Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c56c2b51 authored by Yifei Zhang's avatar Yifei Zhang
Browse files

appops: fix race condition for mOpModeWatchers

- Race condition is possible as read/write of its element (ArraySet) is
  not synchronized. Make a copy before dispatching callbacks.

Test: build & boot
Bug: 338949960
Change-Id: I57f726225ba9ab37a532a1f4639df4f5d2c7f233
parent d97268ac
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1102,6 +1102,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                        if (onModeChangedListeners == null) {
                            continue;
                        }
                        onModeChangedListeners = new ArraySet<>(onModeChangedListeners);
                    }
                    for (int i = 0; i < changedUids.length; i++) {
                        final int changedUid = changedUids[i];
@@ -6396,12 +6397,13 @@ public class AppOpsService extends IAppOpsService.Stub {
    }

    private void notifyWatchersOnDefaultDevice(int code, int uid) {
        final ArraySet<OnOpModeChangedListener> modeChangedListenerSet;
        ArraySet<OnOpModeChangedListener> modeChangedListenerSet;
        synchronized (this) {
            modeChangedListenerSet = mOpModeWatchers.get(code);
            if (modeChangedListenerSet == null) {
                return;
            }
            modeChangedListenerSet = new ArraySet<>(modeChangedListenerSet);
        }
        notifyOpChanged(modeChangedListenerSet,  code, uid, null, PERSISTENT_DEVICE_ID_DEFAULT);
    }