Fix BackgroundDexOptServiceIntegrationTests

- Ensure that calling uid is shell.
- Clear calling identity such that we don't get permission
failures when calling getCurrentUser()

Test: self
Exempt-From-Owner-Approval: cherry-pick
Merged-In: Ifbaceb47edbbc4a6b002d49411ca4635ffc33a08
Change-Id: Ifbaceb47edbbc4a6b002d49411ca4635ffc33a08
Fixes: 111798412
This commit is contained in:
Jorim Jaggi
2018-08-09 16:23:22 +02:00
committed by Andreas Gampe
parent 9cbb19d0a8
commit 3752c462ef
2 changed files with 34 additions and 21 deletions

View File

@@ -9004,6 +9004,20 @@ public class PackageManagerService extends IPackageManager.Stub
}
}
/**
* Enforces that only the system UID or shell's UID can call a method exposed
* via Binder.
*
* @param message used as message if SecurityException is thrown
* @throws SecurityException if the caller is not system or shell
*/
private static void enforceSystemOrShell(String message) {
final int uid = Binder.getCallingUid();
if (uid != Process.SYSTEM_UID && uid != Process.SHELL_UID) {
throw new SecurityException(message);
}
}
@Override
public void performFstrimIfNeeded() {
enforceSystemOrRoot("Only the system can request fstrim");
@@ -9498,7 +9512,13 @@ public class PackageManagerService extends IPackageManager.Stub
if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
return false;
}
return BackgroundDexOptService.runIdleOptimizationsNow(this, mContext, packageNames);
enforceSystemOrShell("runBackgroundDexoptJob");
final long identity = Binder.clearCallingIdentity();
try {
return BackgroundDexOptService.runIdleOptimizationsNow(this, mContext, packageNames);
} finally {
Binder.restoreCallingIdentity(identity);
}
}
private static List<SharedLibraryInfo> findSharedLibraries(PackageParser.Package p) {

View File

@@ -17,8 +17,10 @@
package com.android.server.pm;
import android.app.AlarmManager;
import android.app.UiAutomation;
import android.content.Context;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.os.storage.StorageManager;
import android.support.test.InstrumentationRegistry;
@@ -34,6 +36,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -141,27 +144,17 @@ public final class BackgroundDexOptServiceIntegrationTests {
// Run the command and return the stdout.
private static String runShellCommand(String cmd) throws IOException {
Log.i(TAG, String.format("running command: '%s'", cmd));
long startTime = System.nanoTime();
Process p = Runtime.getRuntime().exec(cmd);
int res;
try {
res = p.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
ParcelFileDescriptor pfd = InstrumentationRegistry.getInstrumentation().getUiAutomation()
.executeShellCommand(cmd);
byte[] buf = new byte[512];
int bytesRead;
FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
StringBuilder stdout = new StringBuilder();
while ((bytesRead = fis.read(buf)) != -1) {
stdout.append(new String(buf, 0, bytesRead));
}
String stdout = inputStreamToString(p.getInputStream());
String stderr = inputStreamToString(p.getErrorStream());
long elapsedTime = System.nanoTime() - startTime;
Log.i(TAG, String.format("ran command: '%s' in %d ms with return code %d", cmd,
TimeUnit.NANOSECONDS.toMillis(elapsedTime), res));
Log.i(TAG, "stdout");
Log.i(TAG, stdout);
Log.i(TAG, "stderr");
Log.i(TAG, stderr);
if (res != 0) {
throw new RuntimeException(String.format("failed command: '%s'", cmd));
}
return stdout;
fis.close();
return stdout.toString();
}
// Run the command and return the stdout split by lines.