| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm.parsing; |
| 18 |
|
| 19 |
import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME; |
| 20 |
import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED; |
| 21 |
import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER; |
| 22 |
|
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.app.admin.DeviceAdminReceiver; |
| 25 |
import android.content.pm.PackageInfo; |
| 26 |
import android.content.pm.PackageManager; |
| 27 |
import android.content.pm.SigningDetails; |
| 28 |
import android.content.pm.VerifierInfo; |
| 29 |
import android.content.pm.parsing.result.ParseInput; |
| 30 |
import android.content.pm.parsing.result.ParseResult; |
| 31 |
import android.content.res.ApkAssets; |
| 32 |
import android.content.res.XmlResourceParser; |
| 33 |
import android.os.Build; |
| 34 |
import android.os.Trace; |
| 35 |
import android.text.TextUtils; |
| 36 |
import android.util.ArrayMap; |
| 37 |
import android.util.ArraySet; |
| 38 |
import android.util.AttributeSet; |
| 39 |
import android.util.Pair; |
| 40 |
import android.util.Slog; |
| 41 |
|
| 42 |
import com.android.internal.util.ArrayUtils; |
| 43 |
|
| 44 |
import libcore.io.IoUtils; |
| 45 |
|
| 46 |
import org.xmlpull.v1.XmlPullParser; |
| 47 |
import org.xmlpull.v1.XmlPullParserException; |
| 48 |
|
| 49 |
import java.io.File; |
| 50 |
import java.io.FileDescriptor; |
| 51 |
import java.io.IOException; |
| 52 |
import java.security.PublicKey; |
| 53 |
import java.util.ArrayList; |
| 54 |
import java.util.Arrays; |
| 55 |
import java.util.Comparator; |
| 56 |
import java.util.List; |
| 57 |
import java.util.Objects; |
| 58 |
import java.util.Set; |
| 59 |
|
| 60 |
|
| 61 |
public class ApkLiteParseUtils { |
| 62 |
|
| 63 |
private static final String TAG = "ApkLiteParseUtils"; |
| 64 |
|
| 65 |
private static final int PARSE_DEFAULT_INSTALL_LOCATION = |
| 66 |
PackageInfo.INSTALL_LOCATION_UNSPECIFIED; |
| 67 |
|
| 68 |
private static final Comparator<String> sSplitNameComparator = new SplitNameComparator(); |
| 69 |
|
| 70 |
public static final String APK_FILE_EXTENSION = ".apk"; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private static final String ANDROID_RES_NAMESPACE = |
| 75 |
"http://schemas.android.com/apk/res/android"; |
| 76 |
private static final int DEFAULT_MIN_SDK_VERSION = 1; |
| 77 |
private static final int DEFAULT_TARGET_SDK_VERSION = 0; |
| 78 |
public static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml"; |
| 79 |
private static final int PARSE_IS_SYSTEM_DIR = 1 << 4; |
| 80 |
private static final int PARSE_COLLECT_CERTIFICATES = 1 << 5; |
| 81 |
private static final int PARSE_FRAMEWORK_RES_SPLITS = 1 << 8; |
| 82 |
private static final String TAG_APPLICATION = "application"; |
| 83 |
private static final String TAG_PACKAGE_VERIFIER = "package-verifier"; |
| 84 |
private static final String TAG_PROFILEABLE = "profileable"; |
| 85 |
private static final String TAG_RECEIVER = "receiver"; |
| 86 |
private static final String TAG_OVERLAY = "overlay"; |
| 87 |
private static final String TAG_USES_SDK = "uses-sdk"; |
| 88 |
private static final String TAG_USES_SPLIT = "uses-split"; |
| 89 |
private static final String TAG_MANIFEST = "manifest"; |
| 90 |
private static final String TAG_SDK_LIBRARY = "sdk-library"; |
| 91 |
private static final int SDK_VERSION = Build.VERSION.SDK_INT; |
| 92 |
private static final String[] SDK_CODENAMES = Build.VERSION.ACTIVE_CODENAMES; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static ParseResult<PackageLite> parsePackageLite(ParseInput input, |
| 104 |
File packageFile, int flags) { |
| 105 |
if (packageFile.isDirectory()) { |
| 106 |
return parseClusterPackageLite(input, packageFile, null, flags); |
| 107 |
} else { |
| 108 |
return parseMonolithicPackageLite(input, packageFile, flags); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static ParseResult<PackageLite> parseMonolithicPackageLite(ParseInput input, |
| 116 |
File packageFile, int flags) { |
| 117 |
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "parseApkLite"); |
| 118 |
try { |
| 119 |
final ParseResult<ApkLite> result = parseApkLite(input, packageFile, flags); |
| 120 |
if (result.isError()) { |
| 121 |
return input.error(result); |
| 122 |
} |
| 123 |
|
| 124 |
final ApkLite baseApk = result.getResult(); |
| 125 |
final String packagePath = packageFile.getAbsolutePath(); |
| 126 |
return input.success( |
| 127 |
new PackageLite(packagePath, baseApk.getPath(), baseApk, null , |
| 128 |
null , null , |
| 129 |
null , null , |
| 130 |
null , baseApk.getTargetSdkVersion(), |
| 131 |
null , null )); |
| 132 |
} finally { |
| 133 |
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public static ParseResult<PackageLite> parseClusterPackageLite(ParseInput input, |
| 145 |
File packageDirOrApk, List<File> frameworkSplits, int flags) { |
| 146 |
final File[] files; |
| 147 |
final boolean parsingFrameworkSplits = (flags & PARSE_FRAMEWORK_RES_SPLITS) != 0; |
| 148 |
if (parsingFrameworkSplits) { |
| 149 |
if (ArrayUtils.isEmpty(frameworkSplits)) { |
| 150 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_NOT_APK, |
| 151 |
"No packages found in split"); |
| 152 |
} |
| 153 |
files = frameworkSplits.toArray(new File[frameworkSplits.size() + 1]); |
| 154 |
|
| 155 |
files[files.length - 1] = packageDirOrApk; |
| 156 |
} else { |
| 157 |
files = packageDirOrApk.listFiles(); |
| 158 |
if (ArrayUtils.isEmpty(files)) { |
| 159 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_NOT_APK, |
| 160 |
"No packages found in split"); |
| 161 |
} |
| 162 |
|
| 163 |
if (files.length == 1 && files[0].isDirectory()) { |
| 164 |
return parseClusterPackageLite(input, files[0], frameworkSplits, flags); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if (parsingFrameworkSplits) { |
| 169 |
|
| 170 |
|
| 171 |
flags = flags & ~PARSE_COLLECT_CERTIFICATES; |
| 172 |
} |
| 173 |
|
| 174 |
String packageName = null; |
| 175 |
int versionCode = 0; |
| 176 |
ApkLite baseApk = null; |
| 177 |
|
| 178 |
final ArrayMap<String, ApkLite> apks = new ArrayMap<>(); |
| 179 |
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "parseApkLite"); |
| 180 |
try { |
| 181 |
for (File file : files) { |
| 182 |
if (isApkFile(file)) { |
| 183 |
final ParseResult<ApkLite> result = parseApkLite(input, file, flags); |
| 184 |
if (result.isError()) { |
| 185 |
return input.error(result); |
| 186 |
} |
| 187 |
|
| 188 |
final ApkLite lite = result.getResult(); |
| 189 |
if (parsingFrameworkSplits && file == files[files.length - 1]) { |
| 190 |
baseApk = lite; |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if (packageName == null) { |
| 196 |
packageName = lite.getPackageName(); |
| 197 |
versionCode = lite.getVersionCode(); |
| 198 |
} else { |
| 199 |
if (!packageName.equals(lite.getPackageName())) { |
| 200 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST, |
| 201 |
"Inconsistent package " + lite.getPackageName() + " in " + file |
| 202 |
+ "; expected " + packageName); |
| 203 |
} |
| 204 |
|
| 205 |
if (!parsingFrameworkSplits && versionCode != lite.getVersionCode()) { |
| 206 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST, |
| 207 |
"Inconsistent version " + lite.getVersionCode() + " in " + file |
| 208 |
+ "; expected " + versionCode); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
if (apks.put(lite.getSplitName(), lite) != null) { |
| 214 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST, |
| 215 |
"Split name " + lite.getSplitName() |
| 216 |
+ " defined more than once; most recent was " + file); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
if (!parsingFrameworkSplits) { |
| 223 |
baseApk = apks.remove(null); |
| 224 |
} |
| 225 |
} finally { |
| 226 |
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 227 |
} |
| 228 |
return composePackageLiteFromApks(input, packageDirOrApk, baseApk, apks); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public static ParseResult<PackageLite> composePackageLiteFromApks(ParseInput input, |
| 241 |
File packageDir, ApkLite baseApk, ArrayMap<String, ApkLite> splitApks) { |
| 242 |
return composePackageLiteFromApks(input, packageDir, baseApk, splitApks, false); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static ParseResult<PackageLite> composePackageLiteFromApks( |
| 256 |
ParseInput input, File packageDir, ApkLite baseApk, |
| 257 |
ArrayMap<String, ApkLite> splitApks, boolean apkRenamed) { |
| 258 |
if (baseApk == null) { |
| 259 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST, |
| 260 |
"Missing base APK in " + packageDir); |
| 261 |
} |
| 262 |
|
| 263 |
final int size = ArrayUtils.size(splitApks); |
| 264 |
|
| 265 |
String[] splitNames = null; |
| 266 |
Set<String>[] requiredSplitTypes = null; |
| 267 |
Set<String>[] splitTypes = null; |
| 268 |
boolean[] isFeatureSplits = null; |
| 269 |
String[] usesSplitNames = null; |
| 270 |
String[] configForSplits = null; |
| 271 |
String[] splitCodePaths = null; |
| 272 |
int[] splitRevisionCodes = null; |
| 273 |
if (size > 0) { |
| 274 |
splitNames = new String[size]; |
| 275 |
requiredSplitTypes = new Set[size]; |
| 276 |
splitTypes = new Set[size]; |
| 277 |
isFeatureSplits = new boolean[size]; |
| 278 |
usesSplitNames = new String[size]; |
| 279 |
configForSplits = new String[size]; |
| 280 |
splitCodePaths = new String[size]; |
| 281 |
splitRevisionCodes = new int[size]; |
| 282 |
|
| 283 |
splitNames = splitApks.keySet().toArray(splitNames); |
| 284 |
Arrays.sort(splitNames, sSplitNameComparator); |
| 285 |
|
| 286 |
for (int i = 0; i < size; i++) { |
| 287 |
final ApkLite apk = splitApks.get(splitNames[i]); |
| 288 |
requiredSplitTypes[i] = apk.getRequiredSplitTypes(); |
| 289 |
splitTypes[i] = apk.getSplitTypes(); |
| 290 |
usesSplitNames[i] = apk.getUsesSplitName(); |
| 291 |
isFeatureSplits[i] = apk.isFeatureSplit(); |
| 292 |
configForSplits[i] = apk.getConfigForSplit(); |
| 293 |
splitCodePaths[i] = apkRenamed ? new File(packageDir, |
| 294 |
splitNameToFileName(apk)).getAbsolutePath() : apk.getPath(); |
| 295 |
splitRevisionCodes[i] = apk.getRevisionCode(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
final String codePath = packageDir.getAbsolutePath(); |
| 300 |
final String baseCodePath = apkRenamed ? new File(packageDir, |
| 301 |
splitNameToFileName(baseApk)).getAbsolutePath() : baseApk.getPath(); |
| 302 |
return input.success( |
| 303 |
new PackageLite(codePath, baseCodePath, baseApk, splitNames, isFeatureSplits, |
| 304 |
usesSplitNames, configForSplits, splitCodePaths, splitRevisionCodes, |
| 305 |
baseApk.getTargetSdkVersion(), requiredSplitTypes, splitTypes)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public static String splitNameToFileName(@NonNull ApkLite apk) { |
| 315 |
Objects.requireNonNull(apk); |
| 316 |
final String fileName = apk.getSplitName() == null ? "base" : "split_" + apk.getSplitName(); |
| 317 |
return fileName + APK_FILE_EXTENSION; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public static ParseResult<ApkLite> parseApkLite(ParseInput input, File apkFile, int flags) { |
| 329 |
return parseApkLiteInner(input, apkFile, null, null, flags); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public static ParseResult<ApkLite> parseApkLite(ParseInput input, |
| 342 |
FileDescriptor fd, String debugPathName, int flags) { |
| 343 |
return parseApkLiteInner(input, null, fd, debugPathName, flags); |
| 344 |
} |
| 345 |
|
| 346 |
private static ParseResult<ApkLite> parseApkLiteInner(ParseInput input, |
| 347 |
File apkFile, FileDescriptor fd, String debugPathName, int flags) { |
| 348 |
final String apkPath = fd != null ? debugPathName : apkFile.getAbsolutePath(); |
| 349 |
|
| 350 |
XmlResourceParser parser = null; |
| 351 |
ApkAssets apkAssets = null; |
| 352 |
try { |
| 353 |
try { |
| 354 |
apkAssets = fd != null |
| 355 |
? ApkAssets.loadFromFd(fd, debugPathName, 0 , null ) |
| 356 |
: ApkAssets.loadFromPath(apkPath); |
| 357 |
} catch (IOException e) { |
| 358 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_NOT_APK, |
| 359 |
"Failed to parse " + apkPath, e); |
| 360 |
} |
| 361 |
|
| 362 |
parser = apkAssets.openXml(ANDROID_MANIFEST_FILENAME); |
| 363 |
|
| 364 |
final SigningDetails signingDetails; |
| 365 |
if ((flags & PARSE_COLLECT_CERTIFICATES) != 0) { |
| 366 |
final boolean skipVerify = (flags & PARSE_IS_SYSTEM_DIR) != 0; |
| 367 |
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "collectCertificates"); |
| 368 |
try { |
| 369 |
final ParseResult<SigningDetails> result = |
| 370 |
FrameworkParsingPackageUtils.getSigningDetails(input, |
| 371 |
apkFile.getAbsolutePath(), |
| 372 |
skipVerify, false, |
| 373 |
SigningDetails.UNKNOWN, DEFAULT_TARGET_SDK_VERSION); |
| 374 |
if (result.isError()) { |
| 375 |
return input.error(result); |
| 376 |
} |
| 377 |
signingDetails = result.getResult(); |
| 378 |
} finally { |
| 379 |
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 380 |
} |
| 381 |
} else { |
| 382 |
signingDetails = SigningDetails.UNKNOWN; |
| 383 |
} |
| 384 |
|
| 385 |
return parseApkLite(input, apkPath, parser, signingDetails, flags); |
| 386 |
} catch (XmlPullParserException | IOException | RuntimeException e) { |
| 387 |
Slog.w(TAG, "Failed to parse " + apkPath, e); |
| 388 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION, |
| 389 |
"Failed to parse " + apkPath, e); |
| 390 |
} finally { |
| 391 |
IoUtils.closeQuietly(parser); |
| 392 |
if (apkAssets != null) { |
| 393 |
try { |
| 394 |
apkAssets.close(); |
| 395 |
} catch (Throwable ignored) { |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
private static ParseResult<ApkLite> parseApkLite(ParseInput input, String codePath, |
| 403 |
XmlResourceParser parser, SigningDetails signingDetails, int flags) |
| 404 |
throws IOException, XmlPullParserException { |
| 405 |
ParseResult<Pair<String, String>> result = parsePackageSplitNames(input, parser); |
| 406 |
if (result.isError()) { |
| 407 |
return input.error(result); |
| 408 |
} |
| 409 |
Pair<String, String> packageSplit = result.getResult(); |
| 410 |
|
| 411 |
final ParseResult<Pair<Set<String>, Set<String>>> requiredSplitTypesResult = |
| 412 |
parseRequiredSplitTypes(input, parser); |
| 413 |
if (requiredSplitTypesResult.isError()) { |
| 414 |
return input.error(result); |
| 415 |
} |
| 416 |
Pair<Set<String>, Set<String>> requiredSplitTypes = requiredSplitTypesResult.getResult(); |
| 417 |
|
| 418 |
int installLocation = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, |
| 419 |
"installLocation", PARSE_DEFAULT_INSTALL_LOCATION); |
| 420 |
int versionCode = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, "versionCode", 0); |
| 421 |
int versionCodeMajor = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, |
| 422 |
"versionCodeMajor", |
| 423 |
0); |
| 424 |
int revisionCode = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, "revisionCode", 0); |
| 425 |
boolean coreApp = parser.getAttributeBooleanValue(null, "coreApp", false); |
| 426 |
boolean isolatedSplits = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 427 |
"isolatedSplits", false); |
| 428 |
boolean isFeatureSplit = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 429 |
"isFeatureSplit", false); |
| 430 |
boolean isSplitRequired = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 431 |
"isSplitRequired", false); |
| 432 |
String configForSplit = parser.getAttributeValue(null, "configForSplit"); |
| 433 |
|
| 434 |
int targetSdkVersion = DEFAULT_TARGET_SDK_VERSION; |
| 435 |
int minSdkVersion = DEFAULT_MIN_SDK_VERSION; |
| 436 |
boolean debuggable = false; |
| 437 |
boolean profilableByShell = false; |
| 438 |
boolean multiArch = false; |
| 439 |
boolean use32bitAbi = false; |
| 440 |
boolean extractNativeLibs = true; |
| 441 |
boolean useEmbeddedDex = false; |
| 442 |
String usesSplitName = null; |
| 443 |
String targetPackage = null; |
| 444 |
boolean overlayIsStatic = false; |
| 445 |
int overlayPriority = 0; |
| 446 |
int rollbackDataPolicy = 0; |
| 447 |
|
| 448 |
String requiredSystemPropertyName = null; |
| 449 |
String requiredSystemPropertyValue = null; |
| 450 |
|
| 451 |
boolean hasDeviceAdminReceiver = false; |
| 452 |
|
| 453 |
boolean isSdkLibrary = false; |
| 454 |
|
| 455 |
|
| 456 |
int type; |
| 457 |
final int searchDepth = parser.getDepth() + 1; |
| 458 |
|
| 459 |
final List<VerifierInfo> verifiers = new ArrayList<>(); |
| 460 |
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 461 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) { |
| 462 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 463 |
continue; |
| 464 |
} |
| 465 |
|
| 466 |
if (parser.getDepth() != searchDepth) { |
| 467 |
continue; |
| 468 |
} |
| 469 |
|
| 470 |
if (TAG_PACKAGE_VERIFIER.equals(parser.getName())) { |
| 471 |
final VerifierInfo verifier = parseVerifier(parser); |
| 472 |
if (verifier != null) { |
| 473 |
verifiers.add(verifier); |
| 474 |
} |
| 475 |
} else if (TAG_APPLICATION.equals(parser.getName())) { |
| 476 |
debuggable = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, "debuggable", |
| 477 |
false); |
| 478 |
multiArch = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, "multiArch", |
| 479 |
false); |
| 480 |
use32bitAbi = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, "use32bitAbi", |
| 481 |
false); |
| 482 |
extractNativeLibs = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 483 |
"extractNativeLibs", true); |
| 484 |
useEmbeddedDex = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 485 |
"useEmbeddedDex", false); |
| 486 |
rollbackDataPolicy = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, |
| 487 |
"rollbackDataPolicy", 0); |
| 488 |
String permission = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 489 |
"permission"); |
| 490 |
boolean hasBindDeviceAdminPermission = |
| 491 |
android.Manifest.permission.BIND_DEVICE_ADMIN.equals(permission); |
| 492 |
|
| 493 |
final int innerDepth = parser.getDepth(); |
| 494 |
int innerType; |
| 495 |
while ((innerType = parser.next()) != XmlPullParser.END_DOCUMENT |
| 496 |
&& (innerType != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { |
| 497 |
if (innerType == XmlPullParser.END_TAG || innerType == XmlPullParser.TEXT) { |
| 498 |
continue; |
| 499 |
} |
| 500 |
|
| 501 |
if (parser.getDepth() != innerDepth + 1) { |
| 502 |
|
| 503 |
continue; |
| 504 |
} |
| 505 |
|
| 506 |
if (TAG_PROFILEABLE.equals(parser.getName())) { |
| 507 |
profilableByShell = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, |
| 508 |
"shell", profilableByShell); |
| 509 |
} else if (TAG_RECEIVER.equals(parser.getName())) { |
| 510 |
hasDeviceAdminReceiver |= isDeviceAdminReceiver( |
| 511 |
parser, hasBindDeviceAdminPermission); |
| 512 |
} else if (TAG_SDK_LIBRARY.equals(parser.getName())) { |
| 513 |
isSdkLibrary = true; |
| 514 |
} |
| 515 |
} |
| 516 |
} else if (TAG_OVERLAY.equals(parser.getName())) { |
| 517 |
requiredSystemPropertyName = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 518 |
"requiredSystemPropertyName"); |
| 519 |
requiredSystemPropertyValue = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 520 |
"requiredSystemPropertyValue"); |
| 521 |
targetPackage = parser.getAttributeValue(ANDROID_RES_NAMESPACE, "targetPackage"); |
| 522 |
overlayIsStatic = parser.getAttributeBooleanValue(ANDROID_RES_NAMESPACE, "isStatic", |
| 523 |
false); |
| 524 |
overlayPriority = parser.getAttributeIntValue(ANDROID_RES_NAMESPACE, "priority", 0); |
| 525 |
} else if (TAG_USES_SPLIT.equals(parser.getName())) { |
| 526 |
if (usesSplitName != null) { |
| 527 |
Slog.w(TAG, "Only one <uses-split> permitted. Ignoring others."); |
| 528 |
continue; |
| 529 |
} |
| 530 |
|
| 531 |
usesSplitName = parser.getAttributeValue(ANDROID_RES_NAMESPACE, "name"); |
| 532 |
if (usesSplitName == null) { |
| 533 |
return input.error(PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, |
| 534 |
"<uses-split> tag requires 'android:name' attribute"); |
| 535 |
} |
| 536 |
} else if (TAG_USES_SDK.equals(parser.getName())) { |
| 537 |
|
| 538 |
String minSdkVersionString = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 539 |
"minSdkVersion"); |
| 540 |
String targetSdkVersionString = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 541 |
"targetSdkVersion"); |
| 542 |
|
| 543 |
int minVer = DEFAULT_MIN_SDK_VERSION; |
| 544 |
String minCode = null; |
| 545 |
boolean minAssigned = false; |
| 546 |
int targetVer = DEFAULT_TARGET_SDK_VERSION; |
| 547 |
String targetCode = null; |
| 548 |
|
| 549 |
if (!TextUtils.isEmpty(minSdkVersionString)) { |
| 550 |
try { |
| 551 |
minVer = Integer.parseInt(minSdkVersionString); |
| 552 |
minAssigned = true; |
| 553 |
} catch (NumberFormatException ignored) { |
| 554 |
minCode = minSdkVersionString; |
| 555 |
minAssigned = !TextUtils.isEmpty(minCode); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
if (!TextUtils.isEmpty(targetSdkVersionString)) { |
| 560 |
try { |
| 561 |
targetVer = Integer.parseInt(targetSdkVersionString); |
| 562 |
} catch (NumberFormatException ignored) { |
| 563 |
targetCode = targetSdkVersionString; |
| 564 |
if (!minAssigned) { |
| 565 |
minCode = targetCode; |
| 566 |
} |
| 567 |
} |
| 568 |
} else { |
| 569 |
targetVer = minVer; |
| 570 |
targetCode = minCode; |
| 571 |
} |
| 572 |
|
| 573 |
boolean allowUnknownCodenames = false; |
| 574 |
if ((flags & FrameworkParsingPackageUtils.PARSE_APK_IN_APEX) != 0) { |
| 575 |
allowUnknownCodenames = true; |
| 576 |
} |
| 577 |
|
| 578 |
ParseResult<Integer> targetResult = FrameworkParsingPackageUtils.computeTargetSdkVersion( |
| 579 |
targetVer, targetCode, SDK_CODENAMES, input, |
| 580 |
allowUnknownCodenames); |
| 581 |
if (targetResult.isError()) { |
| 582 |
return input.error(targetResult); |
| 583 |
} |
| 584 |
targetSdkVersion = targetResult.getResult(); |
| 585 |
|
| 586 |
ParseResult<Integer> minResult = FrameworkParsingPackageUtils.computeMinSdkVersion( |
| 587 |
minVer, minCode, SDK_VERSION, SDK_CODENAMES, input); |
| 588 |
if (minResult.isError()) { |
| 589 |
return input.error(minResult); |
| 590 |
} |
| 591 |
minSdkVersion = minResult.getResult(); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
if ((flags & FrameworkParsingPackageUtils.PARSE_IGNORE_OVERLAY_REQUIRED_SYSTEM_PROPERTY) |
| 597 |
== 0 && !FrameworkParsingPackageUtils.checkRequiredSystemProperties( |
| 598 |
requiredSystemPropertyName, requiredSystemPropertyValue)) { |
| 599 |
String message = "Skipping target and overlay pair " + targetPackage + " and " |
| 600 |
+ codePath + ": overlay ignored due to required system property: " |
| 601 |
+ requiredSystemPropertyName + " with value: " + requiredSystemPropertyValue; |
| 602 |
Slog.i(TAG, message); |
| 603 |
return input.skip(message); |
| 604 |
} |
| 605 |
|
| 606 |
return input.success( |
| 607 |
new ApkLite(codePath, packageSplit.first, packageSplit.second, isFeatureSplit, |
| 608 |
configForSplit, usesSplitName, isSplitRequired, versionCode, |
| 609 |
versionCodeMajor, revisionCode, installLocation, verifiers, signingDetails, |
| 610 |
coreApp, debuggable, profilableByShell, multiArch, use32bitAbi, |
| 611 |
useEmbeddedDex, extractNativeLibs, isolatedSplits, targetPackage, |
| 612 |
overlayIsStatic, overlayPriority, requiredSystemPropertyName, |
| 613 |
requiredSystemPropertyValue, minSdkVersion, targetSdkVersion, |
| 614 |
rollbackDataPolicy, requiredSplitTypes.first, requiredSplitTypes.second, |
| 615 |
hasDeviceAdminReceiver, isSdkLibrary)); |
| 616 |
} |
| 617 |
|
| 618 |
private static boolean isDeviceAdminReceiver( |
| 619 |
XmlResourceParser parser, boolean applicationHasBindDeviceAdminPermission) |
| 620 |
throws XmlPullParserException, IOException { |
| 621 |
String permission = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 622 |
"permission"); |
| 623 |
if (!applicationHasBindDeviceAdminPermission |
| 624 |
&& !android.Manifest.permission.BIND_DEVICE_ADMIN.equals(permission)) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
|
| 628 |
boolean hasDeviceAdminReceiver = false; |
| 629 |
final int depth = parser.getDepth(); |
| 630 |
int type; |
| 631 |
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 632 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() > depth)) { |
| 633 |
if (type == XmlPullParser.END_TAG |
| 634 |
|| type == XmlPullParser.TEXT) { |
| 635 |
continue; |
| 636 |
} |
| 637 |
if (parser.getDepth() != depth + 1) { |
| 638 |
|
| 639 |
continue; |
| 640 |
} |
| 641 |
if (!hasDeviceAdminReceiver && "meta-data".equals(parser.getName())) { |
| 642 |
String name = parser.getAttributeValue(ANDROID_RES_NAMESPACE, |
| 643 |
"name"); |
| 644 |
if (DeviceAdminReceiver.DEVICE_ADMIN_META_DATA.equals(name)) { |
| 645 |
hasDeviceAdminReceiver = true; |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
return hasDeviceAdminReceiver; |
| 650 |
} |
| 651 |
|
| 652 |
public static ParseResult<Pair<String, String>> parsePackageSplitNames(ParseInput input, |
| 653 |
XmlResourceParser parser) throws IOException, XmlPullParserException { |
| 654 |
int type; |
| 655 |
while ((type = parser.next()) != XmlPullParser.START_TAG |
| 656 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 657 |
} |
| 658 |
|
| 659 |
if (type != XmlPullParser.START_TAG) { |
| 660 |
return input.error(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, |
| 661 |
"No start tag found"); |
| 662 |
} |
| 663 |
if (!parser.getName().equals(TAG_MANIFEST)) { |
| 664 |
return input.error(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, |
| 665 |
"No <manifest> tag"); |
| 666 |
} |
| 667 |
|
| 668 |
final String packageName = parser.getAttributeValue(null, "package"); |
| 669 |
if (!"android".equals(packageName)) { |
| 670 |
final ParseResult<?> nameResult = FrameworkParsingPackageUtils.validateName(input, |
| 671 |
packageName, true, true); |
| 672 |
if (nameResult.isError()) { |
| 673 |
return input.error(INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME, |
| 674 |
"Invalid manifest package: " + nameResult.getErrorMessage()); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
String splitName = parser.getAttributeValue(null, "split"); |
| 679 |
if (splitName != null) { |
| 680 |
if (splitName.length() == 0) { |
| 681 |
splitName = null; |
| 682 |
} else { |
| 683 |
final ParseResult<?> nameResult = FrameworkParsingPackageUtils.validateName(input, |
| 684 |
splitName, false, false); |
| 685 |
if (nameResult.isError()) { |
| 686 |
return input.error(INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME, |
| 687 |
"Invalid manifest split: " + nameResult.getErrorMessage()); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
return input.success(Pair.create(packageName.intern(), |
| 693 |
(splitName != null) ? splitName.intern() : splitName)); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
public static ParseResult<Pair<Set<String>, Set<String>>> parseRequiredSplitTypes( |
| 700 |
ParseInput input, XmlResourceParser parser) { |
| 701 |
Set<String> requiredSplitTypes = null; |
| 702 |
Set<String> splitTypes = null; |
| 703 |
String value = parser.getAttributeValue(ANDROID_RES_NAMESPACE, "requiredSplitTypes"); |
| 704 |
if (!TextUtils.isEmpty(value)) { |
| 705 |
final ParseResult<Set<String>> result = separateAndValidateSplitTypes(input, value); |
| 706 |
if (result.isError()) { |
| 707 |
return input.error(result); |
| 708 |
} |
| 709 |
requiredSplitTypes = result.getResult(); |
| 710 |
} |
| 711 |
|
| 712 |
value = parser.getAttributeValue(ANDROID_RES_NAMESPACE, "splitTypes"); |
| 713 |
if (!TextUtils.isEmpty(value)) { |
| 714 |
final ParseResult<Set<String>> result = separateAndValidateSplitTypes(input, value); |
| 715 |
if (result.isError()) { |
| 716 |
return input.error(result); |
| 717 |
} |
| 718 |
splitTypes = result.getResult(); |
| 719 |
} |
| 720 |
|
| 721 |
return input.success(Pair.create(requiredSplitTypes, splitTypes)); |
| 722 |
} |
| 723 |
|
| 724 |
private static ParseResult<Set<String>> separateAndValidateSplitTypes(ParseInput input, |
| 725 |
String values) { |
| 726 |
final Set<String> ret = new ArraySet<>(); |
| 727 |
for (String value : values.trim().split(",")) { |
| 728 |
final String type = value.trim(); |
| 729 |
|
| 730 |
|
| 731 |
final ParseResult<?> nameResult = FrameworkParsingPackageUtils.validateName(input, type, |
| 732 |
false , true ); |
| 733 |
if (nameResult.isError()) { |
| 734 |
return input.error(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED, |
| 735 |
"Invalid manifest split types: " + nameResult.getErrorMessage()); |
| 736 |
} |
| 737 |
if (!ret.add(type)) { |
| 738 |
Slog.w(TAG, type + " was defined multiple times"); |
| 739 |
} |
| 740 |
} |
| 741 |
return input.success(ret); |
| 742 |
} |
| 743 |
|
| 744 |
public static VerifierInfo parseVerifier(AttributeSet attrs) { |
| 745 |
String packageName = attrs.getAttributeValue(ANDROID_RES_NAMESPACE, "name"); |
| 746 |
String encodedPublicKey = attrs.getAttributeValue(ANDROID_RES_NAMESPACE, "publicKey"); |
| 747 |
|
| 748 |
if (packageName == null || packageName.length() == 0) { |
| 749 |
Slog.i(TAG, "verifier package name was null; skipping"); |
| 750 |
return null; |
| 751 |
} |
| 752 |
|
| 753 |
final PublicKey publicKey = FrameworkParsingPackageUtils.parsePublicKey(encodedPublicKey); |
| 754 |
if (publicKey == null) { |
| 755 |
Slog.i(TAG, "Unable to parse verifier public key for " + packageName); |
| 756 |
return null; |
| 757 |
} |
| 758 |
|
| 759 |
return new VerifierInfo(packageName, publicKey); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
private static class SplitNameComparator implements Comparator<String> { |
| 767 |
@Override |
| 768 |
public int compare(String lhs, String rhs) { |
| 769 |
if (lhs == null) { |
| 770 |
return -1; |
| 771 |
} else if (rhs == null) { |
| 772 |
return 1; |
| 773 |
} else { |
| 774 |
return lhs.compareTo(rhs); |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
public static boolean isApkFile(File file) { |
| 786 |
return isApkPath(file.getName()); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
public static boolean isApkPath(String path) { |
| 796 |
return path.endsWith(APK_FILE_EXTENSION); |
| 797 |
} |
| 798 |
} |