From 15b4517e35ba1003f2e604dae2caa9ad9ef62b0b Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 7 Sep 2018 15:54:38 -0700 Subject: [PATCH] Prevent restoring newer backups into older versions of Signal. Relates to #8184 --- res/values/strings.xml | 1 + .../securesms/RegistrationActivity.java | 31 +++++++++++++------ .../securesms/backup/FullBackupImporter.java | 11 ++++++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index c5eef2dd06..927d647c08 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1335,6 +1335,7 @@ Create backup Enter backup passphrase Restore + Cannot import backups from newer versions of Signal Incorrect backup passphrase Checking... %d messages so far... diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 20ce514bb0..1c58f46b5a 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -366,9 +366,9 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif restoreButton.setIndeterminateProgressMode(true); restoreButton.setProgress(50); - new AsyncTask() { + new AsyncTask() { @Override - protected Boolean doInBackground(Void... voids) { + protected BackupImportResult doInBackground(Void... voids) { try { Context context = RegistrationActivity.this; String passphrase = prompt.getText().toString(); @@ -383,23 +383,32 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif TextSecurePreferences.setBackupEnabled(context, true); TextSecurePreferences.setBackupPassphrase(context, passphrase); - return true; + return BackupImportResult.SUCCESS; + } catch (FullBackupImporter.DatabaseDowngradeException e) { + Log.w(TAG, "Failed due to the backup being from a newer version of Signal.", e); + return BackupImportResult.FAILURE_VERSION_DOWNGRADE; } catch (IOException e) { Log.w(TAG, e); - return false; + return BackupImportResult.FAILURE_UNKNOWN; } } @Override - protected void onPostExecute(@NonNull Boolean result) { + protected void onPostExecute(@NonNull BackupImportResult result) { restoreButton.setIndeterminateProgressMode(false); restoreButton.setProgress(0); restoreBackupProgress.setText(""); - if (result) { - displayInitialView(true); - } else { - Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show(); + switch (result) { + case SUCCESS: + displayInitialView(true); + break; + case FAILURE_VERSION_DOWNGRADE: + Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_backup_failure_downgrade, Toast.LENGTH_LONG).show(); + break; + case FAILURE_UNKNOWN: + Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show(); + break; } } }.execute(); @@ -1115,4 +1124,8 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif this.gcmToken = previous.gcmToken; } } + + private enum BackupImportResult { + SUCCESS, FAILURE_VERSION_DOWNGRADE, FAILURE_UNKNOWN + } } diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java index 066d0b4e1d..206806d8e9 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java @@ -98,7 +98,11 @@ public class FullBackupImporter extends FullBackupBase { EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, count)); } - private static void processVersion(@NonNull SQLiteDatabase db, DatabaseVersion version) { + private static void processVersion(@NonNull SQLiteDatabase db, DatabaseVersion version) throws IOException { + if (version.getVersion() > db.getVersion()) { + throw new DatabaseDowngradeException(db.getVersion(), version.getVersion()); + } + db.setVersion(version.getVersion()); } @@ -328,4 +332,9 @@ public class FullBackupImporter extends FullBackupBase { } } + public static class DatabaseDowngradeException extends IOException { + DatabaseDowngradeException(int currentVersion, int backupVersion) { + super("Tried to import a backup with version " + backupVersion + " into a database with version " + currentVersion); + } + } }