The State of Android Application Security and Best Practices

Gone are the days when one could deploy poorly developed code and not many seemed to care. Nowadays when an application is published, it is released to what we call a ‘high threat environment’. This implies that developer anonymity on platform security features during development is not entirely possible. 

Why do we need to worry about security? Let’s say when a user downloads an application from Google Play to his / her device that requires sensitive data as part of the registration process, Google Play provides a certain degree of assurance. But when an application is downloaded from unknown sources, then Google Play security policies are not applicable. 

Due to the open source nature of Android, the platform offers robust security architecture. Android has security features built into the operating system that significantly reduce the frequency and impact of application security issues, and is designed to build apps with the default system based on Linux file permissions. To understand the Android security architecture, let’s look at the three basic Android building blocks:

  • Device Hardware: Android is not just designed to work on smartphones; it runs on a variety of hardware configurations. While it is processor agnostic, it does take advantage of some hardware-specific security capabilities such as ARM v6 eXecute-Never
  • Operating System: The core operating system is built on top of the Linux kernel, and the basic access controls on resources are imposed by the operating system itself
  • Application Runtime: Prior to 4.4 KitKat, Android was using Dalvik virtual machine as the application runtime, which was later replaced by its successor ART (Android Runtime). Both Dalvik and ART include a set of native libraries run inside the security environment which is contained within the Application Sandbox. Applications get a dedicated part of the file system in which private data can be written, including databases and raw files.

Application Signing
Android applications must be digitally signed before they are published for users to download from the marketplace. A digital signature is a cryptographic construct that identifies the developer as the genuine author of the software. Unlike websites, Android apps don’t need any digital certificate from any authorities. Developers can generate self-signed certificates using the standard Java keytool command line utility by providing a private key.

The above command line generates a key ‘release.keystore’ that can be used to sign and publish the resultant .APK file. If you're using the development IDEs such as Eclipse or Android studio, this process is inbuilt as a simple two-step wizard.

Android Permission Model
Android operating system allows installed applications to interact with each other. For example, if you have a camera application installed on your device, any other app can invoke your camera and capture a photo by triggering the required intent method. The installed applications can share resources and data by imposing the permissions they need for additional capabilities.

Permissions are divided into nine groups, allowing users to grant all permissions encompassed by a single action. For instance, granting permission to Contacts includes the ability to both view and edit Contacts.

Permission

Description

Calendar

Managing calendars

Camera

Taking photos and recording videos

Contacts

Managing contacts

Location

Current device location

Microphone

Audio recording

Phone

Dialing and managing phone calls

Body Sensors

Heart rate and similar data

SMS

Sending and viewing messages

Storage

Accessing photos, media, and files


Android system seeks users’ permission while an app is being installed. It asks users to accept or reject all permissions used by the application during app installation. Developers must be careful while requesting the number of permissions required for an app. Not having access to sensitive permissions reduces the risk of inadvertently misusing those permissions, can improve user adoption, and makes your app less vulnerable for attackers.

Although Android’s ‘use It or leave It’ permission model works well, it requires user attention during installation. Users need to invest time to read through the permissions. With the introduction to Android 6.0 Marshmallow, Google simplifies the permissions model to streamline the process for users to install and upgrade apps, as now permissions have to be given on demand.

Securing Application Components
Android apps are composed of components such as Activity, Service and Content Provider, etc. Each component can be secured in a slightly different way.

Securing activities
Activities are the basic components of Android apps. Securing Android activities is straightforward as we can impose certain permissions to start an activity. Then declare the permissions by adding the permission attribute to the specific activity declaration in AndroidManifest.

Notice that in the above code snippet we have declared "com.ness.myapp.permission.START_ACTIVITY" permission to start MainActivity. Without that permission, a call to start the activity will throw a SecurityException.

Securing services
Services in Android are used for background processing. Like activity, a service can be made restricted access by declaring permissions to AndroidManifest. The following code snippet depicts how to declare permission for service.

This service can be started by calling startService() and stopped when stopService() is called. The call to those methods would succeed only if the caller has the VIDEO_DOWNLOAD permission, otherwise it will fail with a SecurityException.

Securing content providers
Content providers are the standard way for apps to make data available to other apps. They allow adding two different permissions, one for reading and other for writing. For example, to declare a content provider that requires separate permissions to read or write data, the entry in the manifest would look like this:

Protecting Stored Data
As Android is ground up from Linux; the operating system imposes basic access control on file system. However, often it is not enough to protect against data vulnerability. Developers must take necessary steps such as using the cryptography techniques to reduce the data security threat. Data can be encrypted before saving into the file system to ensure hackers don’t get it in free form. Different cryptography algorithms are available providing distinct advantage. The symmetric, asymmetric encryption and key hashing, etc. are few cryptography techniques used largely by major players. Both symmetric and asymmetric cryptography algorithms use a key while encrypting the data and the same key is used for decoding the stored data back into its original form. The key used must be uniquely generated and protected. 

A common approach is to use the SecureRandom for generating the deterministic key, which would then be used to encrypt local credential caches. This approach is easy to implement but considered as an anti-pattern. The generated SecureRandom can then be persisted using Android internal storage with MODE_PRIVATE flag. 

Yet another reasonable approach is to generate a truly random AES key when the application is first launched and safeguard it in internal storage. SecureRandom and AES generated key works well, but for additional assurance it’s better to ask users to enter a PIN passphrase to access the application. Most banking applications are developed using this approach. 

Recommendations

  • Focus app security initiatives on protecting the integrity of mobile apps against tampering / reverse-engineering attacks, in addition to traditional approaches to avoid vulnerabilities
  • Be protective about the application components: it can either be a simple activity, background service or a content provider
  • Take aforementioned considerations about the set of permissions an application is using. Using unnecessary permissions can be a reason for users to not download your application. Review the permissions before you’re finished app is pushed into the wild
  • Take early decision for the cryptographic algorithms to be used for persisting sensitive data on device storage
  • Declare custom permissions for application components if required

By Nilanchala Panigrahy on Sep 4, 2015 1:38:07 AM

A bit of a tech freak and a blogger, author and a software developer, Nilanchala is a thought leader in the fusion of design and mobile technologies.

0 Comments

Words: Android, security

Sign up for our Newsletter