Shared storage cannot protect your application from code injection attacks

在动态加载dex的时候可能会出现以下问题:

 java.lang.IllegalArgumentException: Optimized data directory /storage/emulated/0 is not owned by the current user. Shared storage cannot protect your application from code injection attacks.


原因:

从Android4.1.2开始,App不能直接从sdcard中加载字节码,因为sdcard对于所有应用程序来说是共享的,为了防止恶意的代码注入,当采用DexClassLoader从sdcard中动态加载字节码的时候,就会抛出异常,为了安全,在DexFile下加入了以下代码判断

Libcore.os.getuid() != Libcore.os.stat(parent).st_uid

用来判断两个程序是不是同一个uid


DexFile:

private DexFile(String sourceName, String outputName, int flags) throws IOException {
        if (outputName != null) {
            try {
                String parent = new File(outputName).getParent();
                if (Libcore.os.getuid() != Libcore.os.stat(parent).st_uid){
                    throw new IllegalArgumentException("Optimized data directory " + parent
                            + " is not owned by the current user. Shared storage cannot protect"
                            + " your application from code injection attacks.");
                }
            } catch (ErrnoException ignored) {
                // assume we'll fail with a more contextual error later
            }
        }
 
        mCookie = openDexFile(sourceName, outputName, flags);
        mFileName = sourceName;
        guard.open("close");
        //System.out.println("DEX FILE cookie is " + mCookie);
    }


解决方法:

指定dexoutputpath为APP自己的缓存目录,通过context.getDir(“dex”,0)。作为dex的加载目录。

也就是用app的私有目录,data/data/应用程序包名下创建一个dex目录放置该dex,并作为加载目录。

加入以下代码即可:

File dexOutputDir = context.getDir("dex", 0);

DexClassLoader loader= new DexClassLoader(dexpath, dexOutputDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader().getParent());





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值