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或者默認,保證數據只能被當前應用讀取。