打开文件Android Marshmallow时FileNotFound EACCESS

Hyp*_*cle 1 permissions android file android-6.0-marshmallow

在我的产品的自动化测试期间,我将文件推送到应用程序专用目录和chmod文件,以便应用程序可以访问它.在API <Marshmallow,这很好,应用程序从来没有访问文件的问题.

现在,在某些文件上,我尝试打开它时会收到以下日志:

W/System.err(7669): Caused by: 
java.io.FileNotFoundException: /data/user/0/foo/foo.txt: open failed: EACCES (Permission denied) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.IoBridge.open(IoBridge.java:452) 09-28 18:20:56.724: 
W/System.err(7669):     at java.io.FileInputStream.<init>(FileInputStream.java:76) 09-28 18:20:56.724: 
W/System.err(7669):     at android.app.ContextImpl.openFileInput(ContextImpl.java:384) 09-28 18:20:56.724: 
W/System.err(7669):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:177) 09-28 18:20:56.724: 
W/System.err(7669): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.Posix.open(Native Method) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.IoBridge.open(IoBridge.java:438) 09-28 18:20:56.716: 
W/pool-5-thread-1(8279): type=1400 audit(0.0:57): avc: denied { search } for name="files" dev="mtdblock1" ino=7314 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=dir permissive=0
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Hyp*_*cle 6

Android Marshmallow采用了其底层操作系统SELinux(安全增强型Linux)的扩展文件属性,因此在应用程序之间通过shell推送文件和首选项会出现问题,而不是通过对FS具有权限的应用程序.

首先,确保您chmod正确的文件.然后,确保文件也正确拥有.您可以通过ls -la file将有问题的文件组更改为您从上一个命令中找到的内容来检查普通文件的所有者和组的外观.chgrp user.group file

$ ls -la  
drwxrwxrwx u0_a69   u0_a69            2015-09-28 18:20 existing_file
drwxrwxrwx root   root            2015-09-28 18:20 file    
$ chown u0_a69.u0_a69 file
Run Code Online (Sandbox Code Playgroud)

如果这些都不起作用,请检查扩展文件属性ls -Z file并阅读https://fedoraproject.org/wiki/Security_context?rd=SELinux/SecurityContext以了解您正在查看的内容,然后使用chcon some:extended:security:attributes file更改文件权限.

$ ls -Z 
 drwxrwx--x u0_a69   u0_a69 u:object_r:app_data_file:s0:c512,c768 existing_file    
 drwxrwx--x u0_a69   u0_a69 u: file
$ chcon u:object_r:app_data_file:s0:c512,c768 file
Run Code Online (Sandbox Code Playgroud)

如果仍然不起作用,则会/system/bin调用一个二进制setenforce文件来禁用Marshmallow底层SELinux的扩展文件系统安全性.

setenforce permissive从root用户调用,您的应用程序将能够访问所需的文件.这可能会掩盖错误,因此请谨慎使用.

$ setenforce permissive
Run Code Online (Sandbox Code Playgroud)