malloc分配的内存保护标志是什么? [英] What's the protection flags of memory allocated by malloc?

查看:84
本文介绍了malloc分配的内存保护标志是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此线程,至少由malloc分配的内存具有PROT_READ | PROT_EXEC,否则无法执行受约束的函数.

According to this thread,memory allocated by malloc at least have PROT_READ | PROT_EXEC,otherwise the contaned function can't be executed .

man malloc没有提及任何有关保护的内容,因此是一个问题.

man malloc doesn't mention anything about protection thus the question.

推荐答案

malloc不是为代码分配内存的正确工具.您应该使用mmap,并且根据系统上的偏执性安全策略,可能还需要使用mprotect来更改权限.

malloc is not the right tool for allocating memory for code. You should use mmap, and depending on the paranoid security policies on your system, you might need to use mprotect too for changing the permissions.

malloc不是正确工具的原因:

Among the reasons malloc is not the right tool:

  • 仅使用页面粒度设置权限,但是malloc获得的内存不太可能是页面对齐的,因此您最终也将对相邻内存设置权限,这可能会破坏事情.
  • 如果在调用free之前不恢复旧的权限,则可能会破坏malloc的内部.
  • Permissions are set only with page granularity, but memory obtained by malloc is unlikely to be page-aligned, and thus you'll end up setting permissions on adjacent memory too, possibly breaking things.
  • If you don't restore the old permissions before calling free, you might break malloc's internals.

这篇关于malloc分配的内存保护标志是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆