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

Commit 4728d211 authored by Rubin Xu's avatar Rubin Xu
Browse files

Make a copy of data stored in LockSettingsStorage cache

In general the cache should store copies of the original data
to prevent the caller from accidentally corrupting the cache after
the data is cached. This wasn't an issue until recently when
LockSettingsService starts to aggressively zeroize buffers,
which caused some unit test failures.

Bug: 120484642
Test: atest frameworks/base/services/tests/servicestests/src/com/android/server/locksettings/
Change-Id: I8cc61789f19613cca8ed36ca34aa42fe6b2ad674
parent bbe19695
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
@@ -49,6 +48,7 @@ import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@@ -808,7 +808,7 @@ class LockSettingsStorage {
        }

        byte[] peekFile(String fileName) {
            return (byte[]) peek(CacheKey.TYPE_FILE, fileName, -1 /* userId */);
            return copyOf((byte[]) peek(CacheKey.TYPE_FILE, fileName, -1 /* userId */));
        }

        boolean hasFile(String fileName) {
@@ -816,11 +816,11 @@ class LockSettingsStorage {
        }

        void putFile(String key, byte[] value) {
            put(CacheKey.TYPE_FILE, key, value, -1 /* userId */);
            put(CacheKey.TYPE_FILE, key, copyOf(value), -1 /* userId */);
        }

        void putFileIfUnchanged(String key, byte[] value, int version) {
            putIfUnchanged(CacheKey.TYPE_FILE, key, value, -1 /* userId */, version);
            putIfUnchanged(CacheKey.TYPE_FILE, key, copyOf(value), -1 /* userId */, version);
        }

        void setFetched(int userId) {
@@ -868,6 +868,10 @@ class LockSettingsStorage {
            mVersion++;
        }

        private byte[] copyOf(byte[] data) {
            return data != null ? Arrays.copyOf(data, data.length) : null;
        }

        synchronized void purgePath(String path) {
            for (int i = mCache.size() - 1; i >= 0; i--) {
                CacheKey entry = mCache.keyAt(i);