-
-
Notifications
You must be signed in to change notification settings - Fork 466
Fix potential Privilege Escalation via Content Provider (CVE-2018-9492) #2466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
ac51a14
1752102
d8272e2
f90d855
b7a40d7
5931c51
116ad0f
1ba566b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.sentry.android.core.internal.util; | ||
|
|
||
| import android.annotation.SuppressLint; | ||
| import android.content.ContentProvider; | ||
| import android.net.Uri; | ||
| import android.os.Build; | ||
| import io.sentry.NoOpLogger; | ||
| import io.sentry.android.core.BuildInfoProvider; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class ContentProviderSecurityChecker { | ||
|
|
||
| private final @NotNull BuildInfoProvider buildInfoProvider; | ||
|
|
||
| public ContentProviderSecurityChecker() { | ||
| this(new BuildInfoProvider(NoOpLogger.getInstance())); | ||
| } | ||
|
|
||
| public ContentProviderSecurityChecker(final @NotNull BuildInfoProvider buildInfoProvider) { | ||
| this.buildInfoProvider = buildInfoProvider; | ||
| } | ||
|
|
||
| /** | ||
| * Protects against "Privilege Escalation via Content Provider" (CVE-2018-9492). | ||
| * | ||
| * <p>Throws a SecurityException if the security check is breached. | ||
| * | ||
| * <p>See https://www.cvedetails.com/cve/CVE-2018-9492/ and | ||
| * https://github.com/getsentry/sentry-java/issues/2460 | ||
| * | ||
| * <p>Call this function in the {@link ContentProvider#query(Uri, String[], String, String[], | ||
| * String)} function. | ||
| * | ||
| * <p>This should be invoked regardless of whether there is data to query or not. The attack is | ||
| * not contained to the specific provider but rather the entire system. | ||
| * | ||
| * <p>This blocks the attacker by only allowing the app itself (not other apps) to "query" the | ||
| * provider. | ||
| * | ||
| * <p>The vulnerability is specific to un-patched versions of Android 8 and 9 (API 26 to 28). | ||
| * Therefore, this security check is limited to those versions to mitigate risk of regression. | ||
| */ | ||
| @SuppressLint("NewApi") | ||
| public void checkPrivilegeEscalation(ContentProvider contentProvider) { | ||
| final int sdkVersion = buildInfoProvider.getSdkInfoVersion(); | ||
| if (sdkVersion >= Build.VERSION_CODES.O && sdkVersion <= Build.VERSION_CODES.P) { | ||
|
|
||
| String callingPackage = contentProvider.getCallingPackage(); | ||
| String appPackage = contentProvider.getContext().getPackageName(); | ||
| if (callingPackage != null && callingPackage.equals(appPackage)) { | ||
| return; | ||
| } | ||
| throw new SecurityException("Provider does not allow for granting of Uri permissions"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||
| package io.sentry.android.core.internal.util | ||||||||
|
|
||||||||
| import android.content.ContentProvider | ||||||||
| import android.content.Context | ||||||||
| import android.os.Build | ||||||||
| import io.sentry.android.core.BuildInfoProvider | ||||||||
| import org.mockito.kotlin.mock | ||||||||
| import org.mockito.kotlin.verifyNoInteractions | ||||||||
| import org.mockito.kotlin.whenever | ||||||||
| import kotlin.test.Test | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @markushi, ahh! This is what I've been looking for! Thanks! I will use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! b7a40d7 |
||||||||
| import kotlin.test.assertFailsWith | ||||||||
|
|
||||||||
| class ContentProviderSecurityCheckerTest { | ||||||||
|
|
||||||||
| private class Fixture { | ||||||||
| val buildInfoProvider = mock<BuildInfoProvider>() | ||||||||
|
|
||||||||
| fun getSut( | ||||||||
| sdkVersion: Int = Build.VERSION_CODES.O | ||||||||
| ): ContentProviderSecurityChecker { | ||||||||
| whenever(buildInfoProvider.sdkInfoVersion).thenReturn(sdkVersion) | ||||||||
|
|
||||||||
| return ContentProviderSecurityChecker( | ||||||||
| buildInfoProvider | ||||||||
| ) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private val fixture = Fixture() | ||||||||
|
|
||||||||
| @Test | ||||||||
| fun `When sdk version is less than vulnerable versions, security check is not performed`() { | ||||||||
| val contentProvider = mock<ContentProvider>() | ||||||||
|
|
||||||||
| fixture.getSut(Build.VERSION_CODES.N_MR1).checkPrivilegeEscalation(contentProvider) | ||||||||
|
|
||||||||
| verifyNoInteractions(contentProvider) | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| fun `When sdk version is greater than vulnerable versions, security check is not performed`() { | ||||||||
| val contentProvider = mock<ContentProvider>() | ||||||||
|
|
||||||||
| fixture.getSut(Build.VERSION_CODES.Q).checkPrivilegeEscalation(contentProvider) | ||||||||
|
|
||||||||
| verifyNoInteractions(contentProvider) | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| fun `When calling package is null, security check exception is thrown`() { | ||||||||
| val contentProvider = mock<ContentProvider>() | ||||||||
|
|
||||||||
| contentProvider.mockPackages(null) | ||||||||
|
|
||||||||
| assertFailsWith<SecurityException> { | ||||||||
| fixture.getSut().checkPrivilegeEscalation(contentProvider) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| fun `When calling package does not match app package, security check exception is thrown`() { | ||||||||
| val contentProvider = mock<ContentProvider>() | ||||||||
|
|
||||||||
| contentProvider.mockPackages("{$APP_PACKAGE}.attacker") | ||||||||
|
|
||||||||
| assertFailsWith<SecurityException> { | ||||||||
| fixture.getSut().checkPrivilegeEscalation(contentProvider) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| fun `When calling package matches app package, no security exception thrown`() { | ||||||||
| val contentProvider = mock<ContentProvider>() | ||||||||
|
|
||||||||
| contentProvider.mockPackages(APP_PACKAGE) | ||||||||
|
|
||||||||
| fixture.getSut().checkPrivilegeEscalation(contentProvider) | ||||||||
|
|
||||||||
| // No exception! | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private fun ContentProvider.mockPackages(callingPackage: String?) { | ||||||||
| whenever(this.callingPackage).thenReturn(callingPackage) | ||||||||
|
|
||||||||
| val context = mock<Context>() | ||||||||
| whenever(this.context).thenReturn(context) | ||||||||
| whenever(context.packageName).thenReturn(APP_PACKAGE) | ||||||||
| } | ||||||||
|
|
||||||||
| private const val APP_PACKAGE = "com.app" | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could we just create an instance and keep it as a class member instead of creating a new instance of the SecurityChecker every time the method is called? Same for
SentryPerformanceProvider.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could. It's what I would have done if the call-sites were using dependency injection or if we had to use an instance of this class in at least one other function. It's not used anywhere else, nor is this function really invoked (it's only invoke if an attacker tries the exploit) so performance shouldn't be an issue.
I was originally writing a static function but ended up with an SRP class instead for testability and future-proofing in case it needs to be mocked at call-sites.
I'll ignore this since this is a "nit" 😀