BinaryTransparencyService: Fix getOriginalApexPreinstalledLocation

The existing `getOriginalApexPreinstalledLocation` method makes use
of heuristics to determine the filename of originally APEXs
preinstalled on the /system/apex directory.

This change fixes the method to perform prefix comparison instead
of relying on previous heuristics, and thus making this method
more robust overall against package name mismatch, and provides
clear error signals now.

Bug: 259349011
Test: atest BinaryTransparencyServiceTest
Change-Id: Iff6d1041c71e10522f744b9569f5827bb14ed238
This commit is contained in:
Billy Lau
2022-11-28 21:12:23 -06:00
parent 4ae8fc3e60
commit 000011116f

View File

@@ -73,6 +73,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @hide
@@ -104,6 +105,9 @@ public class BinaryTransparencyService extends SystemService {
@VisibleForTesting
static final String BUNDLE_CONTENT_DIGEST = "content-digest";
static final String APEX_PRELOAD_LOCATION = "/system/apex/";
static final String APEX_PRELOAD_LOCATION_ERROR = "could-not-be-determined";
// used for indicating any type of error during MBA measurement
static final int MBA_STATUS_ERROR = 0;
// used for indicating factory condition preloads
@@ -1110,18 +1114,22 @@ public class BinaryTransparencyService extends SystemService {
}
}
// TODO(b/259349011): Need to be more robust against package name mismatch in the filename
@NonNull
private String getOriginalApexPreinstalledLocation(String packageName,
String currentInstalledLocation) {
if (currentInstalledLocation.contains("/decompressed/")) {
String resultPath = "system/apex" + packageName + ".capex";
File f = new File(resultPath);
if (f.exists()) {
return resultPath;
// get a listing of all apex files in /system/apex/
Set<String> originalApexs = Stream.of(new File(APEX_PRELOAD_LOCATION).listFiles())
.filter(f -> !f.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
for (String originalApex : originalApexs) {
if (originalApex.startsWith(packageName)) {
return APEX_PRELOAD_LOCATION + originalApex;
}
return "/system/apex/" + packageName + ".next.capex";
}
return "/system/apex" + packageName + "apex";
return APEX_PRELOAD_LOCATION_ERROR;
}
/**