Merge pull request #6492 from atorralba/atorralba/android-cleartext-storage-database

Java: Create new query Cleartext storage of sensitive information in Android databases
This commit is contained in:
Tony Torralba
2022-02-02 16:23:05 +01:00
committed by GitHub
25 changed files with 679 additions and 45 deletions

View File

@@ -0,0 +1,27 @@
public void sqliteStorageUnsafe(Context ctx, String name, String password) {
// BAD - sensitive information saved in cleartext.
SQLiteDatabase db = ctx.openOrCreateDatabase("test", Context.MODE_PRIVATE, null);
db.execSQL("INSERT INTO users VALUES (?, ?)", new String[] {name, password});
}
public void sqliteStorageSafe(Context ctx, String name, String password) {
// GOOD - sensitive information encrypted with a custom method.
SQLiteDatabase db = ctx.openOrCreateDatabase("test", Context.MODE_PRIVATE, null);
db.execSQL("INSERT INTO users VALUES (?, ?)", new String[] {name, encrypt(password)});
}
public void sqlCipherStorageSafe(String name, String password, String databasePassword) {
// GOOD - sensitive information saved using SQLCipher.
net.sqlcipher.database.SQLiteDatabase db =
net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase("test", databasePassword, null);
db.execSQL("INSERT INTO users VALUES (?, ?)", new String[] {name, password});
}
private static String encrypt(String cleartext) {
// Use an encryption or strong hashing algorithm in the real world.
// The example below just returns a SHA-256 hash.
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(cleartext.getBytes(StandardCharsets.UTF_8));
String encoded = Base64.getEncoder().encodeToString(hash);
return encoded;
}

View File

@@ -0,0 +1,36 @@
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
<qhelp>
<overview>
<p>
SQLite is a lightweight database engine commonly used in Android devices to store data. By itself, SQLite does not offer any encryption mechanism by default and stores all data in cleartext, which introduces a risk if sensitive data like credentials, authentication tokens or personal identifiable information (PII) are directly stored in a SQLite database. The information could be accessed by any process or user in rooted devices, or can be disclosed through chained vulnerabilities, like unexpected access to the private storage through exposed components.
</p>
</overview>
<recommendation>
<p>
Use <code>SQLCipher</code> or similar libraries to add encryption capabilities to SQLite. Alternatively, encrypt sensitive data using cryptographically secure algorithms before storing it in the database.
</p>
</recommendation>
<example>
<p>
In the first example, sensitive user information is stored in cleartext.
</p>
<p>
In the second and third examples, the code encrypts sensitive information before saving it to the database.
</p>
<sample src="CleartextStorageAndroidDatabase.java" />
</example>
<references>
<li>
Android Developers:
<a href="https://developer.android.com/topic/security/data">Work with data more securely</a>
</li>
<li>
SQLCipher:
<a href="https://www.zetetic.net/sqlcipher/sqlcipher-for-android/">Android Application Integration</a>
</li>
</references>
</qhelp>

View File

@@ -0,0 +1,23 @@
/**
* @name Cleartext storage of sensitive information using a local database on Android
* @description Cleartext Storage of Sensitive Information using
* a local database on Android allows access for users with root
* privileges or unexpected exposure from chained vulnerabilities.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/android/cleartext-storage-database
* @tags security
* external/cwe/cwe-312
*/
import java
import semmle.code.java.security.CleartextStorageAndroidDatabaseQuery
from SensitiveSource data, LocalDatabaseOpenMethodAccess s, Expr input, Expr store
where
input = s.getAnInput() and
store = s.getAStore() and
data.flowsTo(input)
select store, "SQLite database $@ containing $@ is stored $@. Data was added $@.", s, s.toString(),
data, "sensitive data", store, "here", input, "here"