Android apps that use SSL/TLS protocols for secure communication should properly verify server certificates. The basic verification includes:

Android SDK  4.0 and later offers packages to implement capabilities to establish network connections. For example, by using java.net, javax.net, android.net or  org.apache.http, a developer can create server sockets or HTTP connection. org.webkit offers functions necessary to implement web browsing capabilities.

A developer has the freedom to customize their SSL implementation, and thus has a responsibility to properly use SSL depending on the intent of the app as well as the environment the apps are used in. If the SSL is not correctly used, a user's sensitive data may leak via the vulnerable SSL communication channel.

Fahl et al [Fahl 2012] summarises the following patterns of the insecure use of SSL:

Trusting All Certificates

* implement the TrustManager interface so that it will trust all  the server certificate (regardless of who signed it, what is the CN etc.)

 

Allowing All Hostnames

* does not verify if the certificate is issued for the URL the client is connecting to

    * when a client connects to example.com, it will accept a server certificate issued for some-other-domain.com

 

Mixed-Mode / No SSL

* A developer mixes secure and insecure connections in the same app or does not use SSL at all.

 

On Android, using HttpURLConnection is recommended to implement HTTP client.

On Android, using HttpURLConnection is recommended for HTTP client implementation.

Noncompliant Code Example

The following code creates a file on the external storage and saves sensitive information to the file:

private String filename = "myfile"

private String string = "sensitive data such as credit card number"
FileOutputStream fos = null;

try {
  file file = new File(getExternalFilesDir(TARGET_TYPE), filename);
  fos = new FileOutputStream(file, false);
  fos.write(string.getBytes());
} catch (FileNotFoundException e) {
  // handle FileNotFoundException
} catch (IOException e) {
  // handle IOException
} finally {
  if (fos != null) {
    try {
  	fos.close();
    } catch (IOException e) {
    }
  }
}

Proof of Concept

Typically, an application stores files in the directory as follows:

/sdcard/Android/data/com.company.app/files/save/appdata/save_appdata

Compliant Solution (Save a File on Internal Storage)

The following code uses the openFileOutput() method to create "myfile" in an application data directory with permission set to MODE_PRIVATE so that other apps cannot access the file:

 

private String filename = "myfile"
private String string = "sensitive data such as credit card number"
FileOutputStream fos = null;

try {
   fos = openFileOutput(filename, Context.MODE_PRIVATE);
   fos.write(string.getBytes());
   fos.close();
} catch (FileNotFoundException e) {
  // handle FileNotFoundException
} catch (IOException e) {
  // handle IOException
} finally {
  if (fos != null) {
    try {
      fos.close();
    } catch (IOException e) {
    }
  }
}

Risk Assessment

Storing sensitive information on external storage can leak sensitive information to malicious apps.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DRD00-J

high

probable

medium

P12

L1

Automated Detection

It is possible to automatically detect whether an application writes to external storage. It is not feasible to automatically determine whether such output could be stored internally.

Related Vulnerabilities

Related Guidelines

Android Secure Coding Guidebook by JSSEC

4.6 Secure File Handling
4.6.1.4 Handling external storage files
4.6.2.1 When creating new files, make them private
4.6.2.2 Don’t create files accessible from other apps with read/write privilege
4.6.2.3 Minimize the use of files stored in external storage such as SD card
4.6.2.4 Consider the lifetime of files when designing apps

Bibliography

[Android API 2013]Class Environment
[JSSEC 2013]4.6 Secure File Handling