diff options
-rw-r--r-- | src/com/android/providers/contacts/CallLogProvider.java | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/src/com/android/providers/contacts/CallLogProvider.java b/src/com/android/providers/contacts/CallLogProvider.java index 991413eb..7d4817cb 100644 --- a/src/com/android/providers/contacts/CallLogProvider.java +++ b/src/com/android/providers/contacts/CallLogProvider.java @@ -36,6 +36,7 @@ import android.database.Cursor; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; +import android.database.sqlite.SQLiteTokenizer; import android.net.Uri; import android.os.Binder; import android.os.Bundle; @@ -81,6 +82,8 @@ import java.nio.file.attribute.FileTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; +import java.util.Set; import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.stream.Collectors; @@ -432,10 +435,11 @@ public class CallLogProvider extends ContentProvider { List<String> pathSegments = uri.getPathSegments(); String phoneNumber = pathSegments.size() >= 2 ? pathSegments.get(2) : null; if (!TextUtils.isEmpty(phoneNumber)) { - qb.appendWhere("PHONE_NUMBERS_EQUAL(number, "); - qb.appendWhereEscapeString(phoneNumber); + qb.appendWhere("PHONE_NUMBERS_EQUAL(number, ?"); qb.appendWhere(mUseStrictPhoneNumberComparation ? ", 1)" : ", 0, " + mMinMatch + ")"); + selectionArgs = copyArrayAndAppendElement(selectionArgs, + "'" + phoneNumber + "'"); } else { qb.appendWhere(Calls.NUMBER_PRESENTATION + "!=" + Calls.PRESENTATION_ALLOWED); @@ -457,12 +461,68 @@ public class CallLogProvider extends ContentProvider { final SQLiteDatabase db = mDbHelper.getReadableDatabase(); final Cursor c = qb.query(db, projection, selectionBuilder.build(), selectionArgs, null, null, sortOrder, limitClause); + + if (match == CALLS_FILTER && selectionArgs.length > 0) { + // throw SE if the user is sending requests that try to bypass voicemail permissions + examineEmptyCursorCause(c, selectionArgs[selectionArgs.length - 1]); + } + if (c != null) { c.setNotificationUri(getContext().getContentResolver(), CallLog.CONTENT_URI); } return c; } + /** + * Helper method for queryInternal that appends an extra argument to the existing selection + * arguments array. + * + * @param oldSelectionArguments the existing selection argument array in queryInternal + * @param phoneNumber the phoneNumber that was passed into queryInternal + * @return the new selection argument array with the phoneNumber as the last argument + */ + private String[] copyArrayAndAppendElement(String[] oldSelectionArguments, String phoneNumber) { + if (oldSelectionArguments == null) { + return new String[]{phoneNumber}; + } + String[] newSelectionArguments = new String[oldSelectionArguments.length + 1]; + System.arraycopy(oldSelectionArguments, 0, newSelectionArguments, 0, + oldSelectionArguments.length); + newSelectionArguments[oldSelectionArguments.length] = phoneNumber; + return newSelectionArguments; + } + + /** + * Helper that throws a Security Exception if the Cursor object is empty && the phoneNumber + * appears to have SQL. + * + * @param cursor returned from the query. + * @param phoneNumber string to check for SQL. + */ + private void examineEmptyCursorCause(Cursor cursor, String phoneNumber) { + // checks if the cursor is empty + if ((cursor == null) || !cursor.moveToFirst()) { + try { + // tokenize the phoneNumber and run each token through a checker + SQLiteTokenizer.tokenize(phoneNumber, SQLiteTokenizer.OPTION_NONE, + this::enforceStrictPhoneNumber); + } catch (IllegalArgumentException e) { + EventLog.writeEvent(0x534e4554, "224771921", Binder.getCallingUid(), + ("invalid phoneNumber passed to queryInternal")); + throw new SecurityException("invalid phoneNumber passed to queryInternal"); + } + } + } + + private void enforceStrictPhoneNumber(String token) { + boolean isAllowedKeyword = SQLiteTokenizer.isKeyword(token); + Set<String> lookupTable = Set.of("UNION", "SELECT", "FROM", "WHERE", + "GROUP", "HAVING", "WINDOW", "VALUES", "ORDER", "LIMIT"); + if (!isAllowedKeyword || lookupTable.contains(token.toUpperCase(Locale.US))) { + throw new IllegalArgumentException("Invalid token " + token); + } + } + private void queryForTesting(Uri uri) { if (!uri.getBooleanQueryParameter(PARAM_KEY_QUERY_FOR_TESTING, false)) { return; |