Proper FBE support: place files in DE

This commit is contained in:
topjohnwu 2017-09-15 18:03:25 +08:00
parent 0acc23e058
commit b4ecd93f1c
4 changed files with 39 additions and 11 deletions

View File

@ -17,7 +17,8 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
android:directBootAware="true"
tools:ignore="UnusedAttribute">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"

View File

@ -136,9 +136,17 @@ public class MagiskManager extends Application {
@Override
public void onCreate() {
super.onCreate();
new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */
prefs = PreferenceManager.getDefaultSharedPreferences(this);
suDB = new SuDatabaseHelper(this);
if (getDatabasePath(SuDatabaseHelper.DB_NAME).exists()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
// Don't migrate yet, wait and check Magisk version
suDB = new SuDatabaseHelper(this);
} else {
// Place the suDB in DE memory
suDB = new SuDatabaseHelper(createDeviceProtectedStorageContext());
}
repoDB = new RepoDatabaseHelper(this);
defaultLocale = Locale.getDefault();
setLocale();
@ -198,6 +206,17 @@ public class MagiskManager extends Application {
boolean hasNetwork = Utils.checkNetworkStatus(this);
getMagiskInfo();
// Check if we need to migrate suDB
if (magiskVersionCode >= 1410
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getDatabasePath(SuDatabaseHelper.DB_NAME).exists()) {
suDB.close();
Context de = createDeviceProtectedStorageContext();
de.moveDatabaseFrom(this, SuDatabaseHelper.DB_NAME);
suDB = new SuDatabaseHelper(de);
}
new LoadLocale(this).exec();
// Root actions

View File

@ -75,7 +75,14 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
MagiskManager mm = getMagiskManager();
if (mm == null) return false;
File install = new File(mm.getApplicationInfo().dataDir, "install");
File install;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Need to be stored in device encrypted storage for FBE
install = new File(mm.createDeviceProtectedStorageContext().getFilesDir().getParent(),
"install");
} else {
install = new File(mm.getApplicationInfo().dataDir, "install");
}
getShell().sh_raw("rm -rf " + install);
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);

View File

@ -39,18 +39,19 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
public static final int NAMESPACE_MODE_REQUESTER = 1;
public static final int NAMESPACE_MODE_ISOLATE = 2;
public static final String DB_NAME = "su.db";
private static final int DATABASE_VER = 3;
private static final String POLICY_TABLE = "policies";
private static final String LOG_TABLE = "logs";
private static final String SETTINGS_TABLE = "settings";
private MagiskManager magiskManager;
private MagiskManager mm;
private PackageManager pm;
private SQLiteDatabase mDb;
public SuDatabaseHelper(Context context) {
super(context, "su.db", null, DATABASE_VER);
magiskManager = Utils.getMagiskManager(context);
super(context, DB_NAME, null, DATABASE_VER);
mm = Utils.getMagiskManager(context);
pm = context.getPackageManager();
mDb = getWritableDatabase();
cleanup();
@ -81,10 +82,10 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
"FROM " + POLICY_TABLE + "_old");
db.execSQL("DROP TABLE " + POLICY_TABLE + "_old");
File oldDB = magiskManager.getDatabasePath("sulog.db");
File oldDB = mm.getDatabasePath("sulog.db");
if (oldDB.exists()) {
migrateLegacyLogList(oldDB, db);
magiskManager.deleteDatabase("sulog.db");
mm.deleteDatabase("sulog.db");
}
++oldVersion;
}
@ -120,7 +121,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
new String[] { String.valueOf(System.currentTimeMillis() / 1000) });
// Clear outdated logs
mDb.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf(
System.currentTimeMillis() - magiskManager.suLogTimeout * 86400000) });
System.currentTimeMillis() - mm.suLogTimeout * 86400000) });
}
public void deletePolicy(Policy policy) {
@ -178,7 +179,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
Policy policy = new Policy(c, pm);
// The application changed UID for some reason, check user config
if (policy.info.uid != policy.uid) {
if (magiskManager.suReauth) {
if (mm.suReauth) {
// Reauth required, remove from DB
deletePolicy(policy);
continue;