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

前端 未结 3 1594
既然无缘
既然无缘 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() will normally return memory with read and write permissions. Some architectures (e.g: older x86) may not allow disabling execute permission in a straightforward way, but that's just a defficiency of the platform.

    If you want to execute code from memory you allocated, you'll have to give execute permissions explicitly, and possibly you'll have to remove write permissions, since having both write and execute permissions on the same memory is considered potentially dangerous on some systems (commonly referred as W^X).

    There have been several other threads on executing code from memory allocated by the programmer:

    Allocate executable ram in c on linux
    Is it possible to execute code from the stack in standard C?

提交回复
热议问题