How to prevent other applications from define same permission name - android

My application define a permission with android:protectionLevel="signature".
<permission android:name="my.app.permission.EXAMPLE" android:protectionLevel="signature" />
My intention is make application modules that can be launched only by my signed app. These application modules have android:permission in its activities.
This works fine. but...
A third-party app can use the same permission name and changed the protection level to normal, like this
<permission android:name="my.app.permission.EXAMPLE" android:protectionLevel="normal" />
If my app is installed first, i can prevent others apps to override the permission. However, if one uninstalls my app and then installs his app it redefines the permission.
Is it possible prevent other application use the same permission name, for example, giving the permission a unique id like application package?
Although the Manifest is encrypted, anyone can read the permission name in log cat when it tries to start the activity that requires this permission (An exception is thrown having the required permission name).

There's no enforcement, only convention. Like the rest of the Java world, it loosely relies on domain name registration infrastructure. The idea is that you prefix your permission name with your public Internet domain name (e. g. com.myawesomecompany.myapp.MYPERMISSION) which you own.
Uniqueness of domain names is enforced by the registrar community, naturally.
Yes, the system is open for abuse.
EDIT: if you're securing a broadcast-based channel, you can add a two-way signature check if you feel like it. Call Context.sendBroadcast() with the permission name as a second parameter.
EDIT2: I feel you're overthinking this while closing your eyes at the bigger Android app security picture. Which is not impressive. Abusing the privilege infrastructure is not how one hacks into an Android app. If I set out to intercept your intents, I won't be putting together a fake intent receiver (activity, service). Instead, I'd connect with a debugger to the genuine receiver in your app, signature and all.
With publicly available tools, it takes minutes to put togther an Eclipse project for a given APK. Load it up into Eclipse, connect to a running process, set breakpoints in relevant system APIs (Android is open source, remember), voila. With a bit of extra effort, you can get decompiled Java sources for an APK and debug in terms of YOUR methods, as opposed to system ones.

copyed from Google Andorid Doc:
Note: The system does not allow multiple packages to declare a permission with the same name, unless all the packages are signed with the same certificate. If a package declares a permission, the system does not permit the user to install other packages with the same permission name, unless those packages are signed with the same certificate as the first package. To avoid naming collisions, we recommend using reverse-domain-style naming for custom permissions, for example com.example.myapp.ENGAGE_HYPERSPACE.
https://developer.android.com/guide/topics/permissions/defining

If you want to prevent other applications from changing your permission level, you can use system predefine permissions which have level "signature". No other regular app can define permission before system.
Use system permission to protect your resource doesn't mean your app have to sign with platform key.
example:
<service
android:name="xxx.xxx.xxx.exservice"
android:permission="android.permission.BROADCAST_PACKAGE_REMOVED" >
The only issue is AppStore would show which permission you use if below code shows in app's manifest.xml
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
In this example, you can access you resource by the same sign key, but definitely you can't broadcast package remove.

Related

Android custom permission vs app signature

I have gone through the available documentation and SO posts on both these topics (custom permissions and app signature), my understanding is:
Apps signed with same key can use signature-level permissions
A custom permission should only be defined in one place and the app declaring the permission needs to be installed first
A custom permission can be defined in multiple apps if the apps share same signature
Now I need to define some components in one app that I will be using in another app signed with the same key, but according to this I don't need to define a custom permission and can place a signature check
If the functionality is only available to apps signed with the same signature as the providing app, you may be able to avoid defining custom permissions by using signature checks. When one of your apps makes a request of another of your apps, the second app can verify that both apps are signed with the same certificate before complying with the request.
I don't understand what is meant by signature checks, is there a way to place a check in manifest on that component or do I have to check it programmatically? If latter, isn't it better to define a custom permission in both apps instead (to negate installation order issue)?
is there a way to place a check in manifest on that component
No, sorry.
do I have to check it programmatically?
Yes. This pair of old projects demonstrate a bound service and a client that checks the signature of the service. Those projects are covered in a chapter in a chapter of this free old book. That sample has problems in modern Android, as an app can have multiple signatures. But, the SiganturesUtil class that it relies on shows how to get signatures from PackageManager for an installed app. You can then compare them with the expected value to determine if you are indeed talking to the app that you expect.
If latter, isn't it better to define a custom permission in both apps instead (to negate installation order issue)?
Signature checks are more flexible — they are not limited to two apps being signed by the same signing key. That, plus historical issues with custom permissions, may be why Google recommends signature checks over custom permissions on that page.

Android duplicate permission

I have an app with free and pro versions. The pro version propose a migration assistant to import data from the free to the pro version when a user buy the pro after using the free.
I added a custom permission to protect the free version data. Both versions have the permission declared in their manifest, and only the pro version has the uses-permission.
It works well on pre-Lollipop devices, but on Lollipop, I get a INSTALL_FAILED_DUPLICATE_PERMISSION error message if I try to install the pro version when the free is installed. It works if I sign both versions with the same key (in debug for instance), but I didn't on the Play Store, and it can't be changed.
So, is there any solution, or are the permissions broken on Lollipop ??
is there any solution
In your case, you may be able to get away with Derek Fung's approach, since there is a clear installation order (few, if any, people will install the free app after installing the paid app).
Alternatively, you can skip permissions, since in this case, there are two apps that need to communicate with each other. You know what those apps are, and you know their signing keys. Perform your own validation as part of your IPC work:
Use Binder.getCallingUid() to get the UID of the calling app.
Use PackageManager and its getPackagesForUid() to find the application ID(s) associated with the UID. In your case, there should be just one, and you can confirm that the application ID is the expected value.
Use my SignatureUtils class (or just grab the code for it) to validate that the signing key of the calling application matches the expected value.
are the permissions broken on Lollipop ?
The behavior that you want — any app can define any other app's permission — is dreadful from a security standpoint.
Such "feature" would also prevent creating / using 3rd party content providers, because if your app is installed first, you need to have the permission's declaration
Custom permissions are designed for cases where there is a clear order of installation (e.g., pre-installed apps, host and plugins). Custom permissions are not well-suited for peer scenarios, where the order of installation is not pre-determined.
In some cases, this can be worked around by grafting in plugins where they might ordinarily not be needed:
App A wants to use the provider published by App B, but App A might be installed first
The author of App A creates a plugin (A') that has the <uses-permission> for App B's provider and mediates communication with App B
App A invokes something on A' when A' is installed (e.g., sends a broadcast to a specific component), so A' can validate that App A and App B were installed before A'
When the user tries to do something that involves A talking to B, if the plugin is not installed, lead the user to install the plugin
Clunky, but it should work.
It is likely that you both of your free and paid app has declared permission of the same name like below.
<permission android:name="com.example.permission" android:label="test_permission" android:protectionLevel="dangerous"></permission>
Edit:
For your case, seems you should only declare your permission on your free version. For your paid version, remove the <permission> tag and leave only the <uses-permission> tag

Custom permission for application

I was recently reading up about custom permission for our application in android.
uses-permission is clear. It contains the permission that your application will need in order to access some of user data or device features, etc and to function properly.
Now, we come to permission element. It declares permissions that activities or services might require other applications hold in order to use your application's data or logic
Now, say I use permission tag in my application's manifest file such as:
<permission android:name="my.pkg.CUST_PER"/>
This will imply that my application may have this possible permission.
And enforce that permission using it in my Activity tag like this:
<activity
android:name=".MyApp"
android:permission="my.pkg.CUST_PER">
Now, as per my understanding, only applications that have requested my indicated permission will be able to access my application's secured components.
If other app tries to access those components without my custom permission, what will happen? I think it should crash, and will that be seen in logcat as:
SecurityException: Missing permission: my.pkg.CUST_PER
If so, isn't that a security breach?
How to protect application data in such a circumstance?
uses-permission is clear. It contains the permission that your application will need in order to access some of user data or device features, etc and to function properly.
<uses-permission> means that your app wishes to hold the permission named in the <uses-permission> element. What is defended by that permission is up to other developers. In some cases, it may be defending some things that allow you "to access some of user data or device features".
This will imply that my application may have this possible permission.
No, it does not. It simply defines a new permission. It does not state that your app, or any other app, has anything else to do with the permission.
Now, as per my understanding, only applications that have requested my indicated permission will be able to access my application's secured components.
More accurately, only apps with the <uses-permission> element could qualify to access the secured component. In addition, as Mr. Orlowski notes, the protectionLevel of the <permission> indicates if user acceptance is involved (a protectinoLevel of normal or dangerous), if the app needs to be signed by the same signing key as the app that is defending itself with the permission (a protectionLevel of signature), or if the app needs to be installed on the /system partition (a protectionLevel of system).
If other app tries to access those components without my custom permission, what will happen? I think it should crash, and will that be seen in logcat as: Requires this permission: my.pkg.CUST_PER
Correct.
If so, isn't that a security breach?
Not particularly.
How to protect application data in such a circumstance?
Don't expose it in the first place. The complete and entire point behind having android:permission is because you want other apps to have access to the data, subject to user acceptance, signature match, etc. If you do not want other apps to have access to the data, do not export the component. For activities, services, and manifest-registered receivers, this is usually accomplished by not having an <intent-filter>. For <provider> elements, you will want to manually have the android:exported attribute set to false.
How to protect application data in such a circumstance?
You should have read whole docs as android:protectionLevel exists exactly to address this problem and is explained here:
https://developer.android.com/guide/topics/manifest/permission-element.html#plevel

Is the manifest overwritten on updates?

I have to make some configuration settings specific to certain builds of my app, in the manifest.
These builds have same package name and code basis as the "normal" ones -> it's the same app. But I need these few little extras in the manifest.
It's clear that I can make that, just by creating 2 different builds with slightly different manifest file.
But: What will happen on app update? Are the manifest files overwritten, such that my extras will be removed?
I think this could not be the case, since I read some infos about special permissions for pre-installed apps. If the whole manifest was overwritten on updates, this would not work.
But I don't know how it works for other special informations in the manifest.
Thanks in advance!
The manifest is replaced when you update your app. This is how apps request or remove permissions, and add new Activities and Broadcast Receivers.
Certain pre installed apps get extra permission not only because of the manifest, but because they are installed in a separate folder on the device, which grants them more permissions.
When an app is updated, your manifest file is replaced.
Your primary question already being answered, I'll answer your secondary one. The special manifest permissions for pre-installed applications are managed by the code signing process. A manifest is allowed to use those permissions if and only if it is in a .apk file that's signed by the same key as the system build itself (e.g., if you're installing on a Samsung phone, it would have to be signed by Samsung's key, and so on). So it's actually less about being pre-installed and more about being authorized by the device manufacturer.

Android apps able to communicate without permissions set

I was under the impressions that two apps were sandboxed and unable to call each other (by intents or contentresolver etc) unless the callee declared and enforced specific permissions and the caller used appropriate uses-permission elements? However, I have developed two apps, one containing a content provider, and another with activities that use the content provider. Neither have permissions declared enforced or used. I deploy them directly from Eclipse to my phone and they are able to use each other.
I have checked that they really are running as separate processes and user ids, and they are. Why am I not seeing security exceptions? Should Linux underneath, by default, stop this communication? They will be signed by the default DEBUG certificate. Does this give them more rights to "talk" to each other, i.e. if I signed with an explicit certificate would the sandboxing kick in?
As soon as I declare and enforce a permission in the content provider app the other app does need the uses-permission to allow communication.
Cheers
Yes, if your apps have the same signature, then they have access to each other. It's similar to package level permissions in java.
http://developer.android.com/guide/topics/security/security.html

Categories

Resources