DO NOT MERGE 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
(cherry picked from commit 4728d2114c)
This commit is contained in:
Rubin Xu
2019-03-15 11:38:36 +00:00
committed by Rich Cannings
parent ddaa6338c8
commit 567f2357f9

View File

@@ -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);