The following table lists the Java development guidelines in the book, "Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs", and states their applicability to the development of Android applications. Applicable means that the guideline can be applied to general Java platforms including Android. Applicable in principle means that the guideline can The top two tables list the Java rules and Java recommendations that are Applicable in principle, meaning that it can be applied to Android but the examples shown in the guideline are not relevant to Android, and in some cases the guideline's full description also needs edits (the latter are provided in the Comments column). Not applicable means that the guideline cannot The third table lists Unknown rules and recommendations, meaning that we have not yet determined if the guideline can be applied to Android platforms.
...
Contents
...
| Table of Contents |
|---|
Rules/Applicable in Principle to Android
Recommendations
| Do not use deprecated or obsolete classes or methods | The Android SDK also has deprecated or obsolete APIs. Also, there may exist incompatible APIs depending on the SDK version. Therefore, it is recommended that developers refer to the "Android API Difference report" and consider replacing the deprecated APIs. |
| MET03-J. Methods that perform a security check must be declared private or final | On Android, System.getSecurityManager() is not used and the use of a Security Manager is not exercised. However, an Android developer can implement security-sensitive methods so the principle may be applicable on Android. |
| ERR09-J. Do not allow untrusted code to terminate the JVM | On Android, System.exit() should not be used because it will terminate the virtual machine abruptly, ignoring the activity lifecycle which may prevent proper garbage collection. |
| LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code | |
| LCK05-J. Synchronize access to static fields that can be modified by untrusted code | |
| LCK11-J. Avoid client-side locking when using classes that do not commit to their locking strategy | |
| THI00-J. Do not invoke Thread.run() | Android provides a couple of solutions for threading. The Android Developers Blog's article "Painless threading" discusses those solutions. |
| THI02-J. Notify all waiting threads rather than a single thread | |
| THI03-J. Always invoke wait() and await() methods inside a loop | |
| THI04-J. Ensure that threads performing blocking operations can be terminated | |
| THI05-J. Do not use Thread.stop() to terminate threads | On Android, Thread.stop() was deprecated in API level 1. |
| TPS00-J. Use thread pools to enable graceful degradation of service during traffic bursts | |
| TPS01-J. Do not execute interdependent tasks in a bounded thread pool | |
| TPS02-J. Ensure that tasks submitted to a thread pool are interruptible | |
| TPS03-J. Ensure that tasks executing in a thread pool do not fail silently | |
| TPS04-J. Ensure ThreadLocal variables are reinitialized when using thread pools | |
| TSM00-J. Do not override thread-safe methods with methods that are not thread-safe | |
| TSM01-J. Do not let the this reference escape during object construction | |
| TSM02-J. Do not use background threads during class initialization | |
| FIO00-J. Do not operate on files in shared directories | On Android, the SD card ( / sdcard or /mnt/ sdcard ) is shared among multiple applications, thus sensitive files should not be stored on the SD card. |
| FIO01-J. Create files with appropriate access permissions | Creating files with weak permissions may allow malicious applications to access the files. |
| FIO04-J. Release resources when they are no longer needed | The compliant solution (Java SE 7: try-with-resources) is not yet supported at API level 18 (Android 4.3). |
| FIO06-J. Do not create multiple buffered wrappers on a single InputStream | |
| FIO13-J. Do not log sensitive information outside a trust boundary | DRD04-J. Do not log sensitive information is an Android specific instance of this rule. |
| FIO14-J. Perform proper cleanup at program termination | Although most of the code examples are not applicable to the Android platform, the principle is applicable to Android. There are a number of ways to terminate a process on Android: android.app.Activity.finish(), and the related finish... methods, android.app.Activity.moveTaskToBack(boolean flag), android.os.Process.killProcess(int pid), System.exit(). |
| FIO15-J. Do not operate on untrusted file links | |
| SEC01-J. Do not allow tainted variables in privileged blocks | The code examples using the java.security package are not applicable to Android but the principle of the rule is applicable to Android apps. |
| SEC02-J. Do not base security checks on untrusted sources | The code examples using the java.security package are not applicable to Android but the principle of the rule is applicable to Android apps. |
| SEC03-J. Do not load trusted classes after allowing untrusted code to load arbitrary classes | On Android, the use of DexClassLoader or PathClassLoader requires caution. |
| SEC05-J. Do not use reflection to increase accessibility of classes, methods, or fields | Reflection can be used on Android so the rule is applicable. Also the use of reflection may allow a developer to access private Android APIs and so requires caution. |
| JNI 03-J. Do not use direct pointers to Java objects in JNI code | Applicable to API versions 14 and above, with NDK versions 7 and above. |
| ENV02-J. Do not trust the values of environment variables | On Android, the environment variable user.name is not used and is left blank. However, environment variables exist and are used on Android so the rule is applicable. |
| ENV04-J. Do not disable bytecode verification | Under the default settings, bytecode verification is enabled on the Dalvik VM. To change the settings use the adb shell to set the appropriate system property, for example: adb shell setprop dalvik.vm.dexopt-flags v=a or pass -Xverify:all as an argument to the Dalvik VM. |
| MSC03-J. Never hard code sensitive information | Hard coded information can be easily obtained on Android by using the apktool to decompile an application or by using dex2jar to convert a dex file to a jar file. |
Recommendations/Applicable in Principle to Android
Guideline | Comments |
|---|---|
| MSC59-J. Limit the lifetime of sensitive data | The non-compliant code example is probably not problematic on Dalvik because each app has its own Dalvik VM and string objects would not be accessible from other apps (?) |
| SEC55-J. Ensure security-sensitive methods are called with validated arguments | On Android, accessControlContext is not available. |
| IDS56-J. Prevent arbitrary file upload | |
| IDS51-J. Properly encode or escape output | |
| IDS52-J. Prevent code injection | ScriptEngineManager is not included in the Android SDK. |
| IDS54-J. Prevent LDAP injection | Applicable in principle for android apps that tries to implement its own LDAP |
| SEC50-J. Avoid granting excess privileges | The brief phrase for the guideline applies to Android. However, the current extended-text description for the guideline in the hardcopy book does not apply to Android, because Android does not use AccessController. The following text supplements that section, to make it applicable to Android.: An application should use as few "<uses-permission>"s in AndroidManifest.xml as possible. App developers should also avoid signature/system/dangerous permissions, and having a shared system UID. System API calls are code running as system, and apps which make system API calls require standard permissions the app must specify in the application manifest with "<uses-permission>". http://developer.android.com/guide/topics/manifest/uses-permission-element.html |
| SEC51-J. Minimize privileged code | The brief phrase for the guideline applies to Android. However, the current extended-text description for the guideline in the hardcopy book does not apply to Android, because Android does not use AccessController. The following text supplements that section, to make it applicable to Android.: Minimize the code running as system, with permissions defined in another app’s manifest, or in shared user ID applications. System API calls are code running as system, and apps which make system API calls require standard permissions the app must specify in the application manifest with "<uses-permission>". Only applications which are signed with the same signature and also request the same sharedUserID are granted a shared user ID. Data/files stored by apps which share a user ID are accessible to all those apps.http://developer.android.com/guide/topics/security/permissions.html http://developer.android.com/guide/topics/manifest/uses-permission-element.html |
| SEC53-J. Define custom security permissions for fine-grained security | The brief phrase for the guideline applies to Android. However, the current extended-text description for the guideline in the hardcopy book does not apply to Android. The following text supplements that section, to make it applicable to Android.: Applications are able to define their own new permissions, to restrict access to their components by other applications. Applications indicate the procedure the system should follow when determining whether to grant another app the permission, depending on protectionLevel – e.g., setting protectionLevel to “signature” so it is automatically granted to other applications requesting the permission which are signed with the same key. In addition to defining their own new permissions, applications can declare the requirement for (self-defined, other-app-defined, or system-defined) permissions, to restrict access to their components by other applications. |
| FIO51-J. Identify files using multiple file attributes | On Android, better to use openFileOutput/openFileInput for file I/O. |
| MSC60-J. Do not use assertions to verify the absence of runtime errors | On Android, assert() is ignored by default. |
| FIO50-J. Do not make assumptions about file creation | On Android, java.nio.file is not available. |
Guideline
Applicable to Android Application Development?
Comments
accessControlContext is not available.ScriptEngineManager is not included in the Android SDK.Object.equals() to compare cryptographic keysSecureRandom is properly seededAccessController. The following text supplements that section, to make it applicable to Android.: An application should use as few "<uses-permission>"s in AndroidManifest.xml as possible. App developers should also avoid signature/system/dangerous permissions, and having a shared system UID. System API calls are code running as system, and apps which make system API calls require standard permissions the app must specify in the application manifest with "<uses-permission>". http://developer.android.com/guide/topics/manifest/uses-permission-element.htmlAccessController. The following text supplements that section, to make it applicable to Android.: Minimize the code running as system, with permissions defined in another app’s manifest, or in shared user ID applications. System API calls are code running as system, and apps which make system API calls require standard permissions the app must specify in the application manifest with "<uses-permission>". Only applications which are signed with the same signature and also request the same sharedUserID are granted a shared user ID. Data/files stored by apps which share a user ID are accessible to all those apps.http://developer.android.com/guide/topics/security/permissions.html
http://developer.android.com/guide/topics/manifest/uses-permission-element.html
protectionLevel – e.g., setting protectionLevel to “signature” so it is automatically granted to other applications requesting the permission which are signed with the same key. In addition to defining their own new permissions, applications can declare the requirement for (self-defined, other-app-defined, or system-defined) permissions, to restrict access to their components by other applications.@SuppressWarnings annotationopenFileOutput/openFileInput for file I/O.try-with-resources statement to safely handle closeable resourcestry-with-resource is not availableassert() is ignored by default.java.nio.file is not available.clone() method calls super.clone()sleep(), yield(), or getState() methods provide synchronization semanticswriteUnshared() and readUnshared() with carenull
Bibliography
| [Long 2013] | Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs |
CERT Oracle Coding Standard for Java