OpenFileOutput存储风险简介
Android应用内部文件是指/data/data/packageName/files路径下的文件,openFileOutput方法可以对内部文件的数据进行读写操作。
通过ContextWrapper类的openFileOutput方法或者Context类的openFileOutput方法中的第二个参数mode设置文件的权限。
- Mode为MODE_PRIVATE或者MODE_APPEND时,其他程序无法对该文件进行操作。
- Mode为MODE_WORLD_READABLE或者MODE_WORLD_WRITEABLE时,其他应用可以读写该文件
/**
* Open a private file associated with this Context's application package
* for writing. Creates the file if it doesn't already exist.
*
* <p>No permissions are required to invoke this method, since it uses internal
* storage.
*
* @param name The name of the file to open; can not contain path
* separators.
* @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the
* default operation, {@link #MODE_APPEND} to append to an existing file,
* {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control
* permissions.
*
* @return The resulting {@link FileOutputStream}.
*
* @see #MODE_APPEND
* @see #MODE_PRIVATE
* @see #MODE_WORLD_READABLE
* @see #MODE_WORLD_WRITEABLE
* @see #openFileInput
* @see #fileList
* @see #deleteFile
* @see java.io.FileOutputStream#FileOutputStream(String)
*/
public abstract FileOutputStream openFileOutput(String name, int mode)
throws FileNotFoundException;
Mode默认值为MODE_PRIVATE,如果文件权限设置为MODE_WORLD_READABLE或者MODE_WORLD_WRITEABLE,就有可能导致敏感信息泄露和信息被篡改。
检测方法
检测调用ContextWrapper类的openFileOutput方法和Context类的openFileOutput方法地方,检查第二个参数Mode,判断是否是MODE_WORLD_READABLE或者MODE_WORLD_WRITEABLE,如果是这两个权限,则认为该应用数据存在被第三方应用读取和篡改风险。
修复方案
如果需要调用openFileOutput保存数据,则权限设置为MODE_PRIVATE或者MODE_APPEND或者默认,保证数据只能被当前应用读取。