What's the protection flags of memory allocated by malloc?

前端 未结 3 1592
既然无缘
既然无缘 2021-01-12 19:39

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

<
3条回答
  •  我在风中等你
    2021-01-12 19:59

    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.

    Among the reasons malloc is not the right tool:

    • 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.

提交回复
热议问题