From a431fea9bb1be153661fab9f78a8672ca62ebacd Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 1 Jul 2021 10:39:54 +0200 Subject: [PATCH] Fix settings provider being called with wrong identity Fixes #1503 --- .../microg/gms/settings/SettingsContract.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/play-services-basement/src/main/kotlin/org/microg/gms/settings/SettingsContract.kt b/play-services-basement/src/main/kotlin/org/microg/gms/settings/SettingsContract.kt index cb87e5eb..53a2ea0f 100644 --- a/play-services-basement/src/main/kotlin/org/microg/gms/settings/SettingsContract.kt +++ b/play-services-basement/src/main/kotlin/org/microg/gms/settings/SettingsContract.kt @@ -4,6 +4,7 @@ import android.content.ContentValues import android.content.Context import android.database.Cursor import android.net.Uri +import android.os.Binder object SettingsContract { const val AUTHORITY = "org.microg.gms.settings" @@ -103,15 +104,24 @@ object SettingsContract { const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id" } - fun getSettings(context: Context, uri: Uri, projection: Array?, f: (Cursor) -> T): T { - context.contentResolver.query(uri, projection, null, null, null).use { c -> - require(c != null) { "Cursor for query $uri ${projection?.toList()} was null" } - if (!c.moveToFirst()) error("Cursor for query $uri ${projection?.toList()} was empty") - return f.invoke(c) + private fun withoutCallingIdentity(f: () -> T): T { + val identity = Binder.clearCallingIdentity() + try { + return f.invoke() + } finally { + Binder.restoreCallingIdentity(identity) } } - fun setSettings(context: Context, uri: Uri, v: ContentValues.() -> Unit) { + fun getSettings(context: Context, uri: Uri, projection: Array?, f: (Cursor) -> T): T = withoutCallingIdentity { + context.contentResolver.query(uri, projection, null, null, null).use { c -> + require(c != null) { "Cursor for query $uri ${projection?.toList()} was null" } + if (!c.moveToFirst()) error("Cursor for query $uri ${projection?.toList()} was empty") + f.invoke(c) + } + } + + fun setSettings(context: Context, uri: Uri, v: ContentValues.() -> Unit) = withoutCallingIdentity { val values = ContentValues().apply { v.invoke(this) } val affected = context.contentResolver.update(uri, values, null, null) require(affected == 1) { "Update for $uri with $values affected 0 rows"}