[英]What's the protection flags of memory allocated by malloc?
According to this thread ,memory allocated by malloc
at least have PROT_READ | PROT_EXEC
根据this thread ,malloc分配的
malloc
至少有PROT_READ | PROT_EXEC
PROT_READ | PROT_EXEC
,otherwise the contaned function can't be executed. PROT_READ | PROT_EXEC
,否则无法执行包含的 function。
man malloc
doesn't mention anything about protection thus the question. man malloc
没有提到任何关于保护的内容,因此这个问题。
malloc
is not the right tool for allocating memory for code. malloc
不是为代码分配 memory 的正确工具。 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.您应该使用
mmap
,并且根据系统上的偏执安全策略,您可能还需要使用mprotect
来更改权限。
Among the reasons malloc
is not the right tool: malloc
不是正确工具的原因包括:
malloc
is unlikely to be page-aligned, and thus you'll end up setting permissions on adjacent memory too, possibly breaking things.malloc
不太可能是页面对齐的,因此您最终会在相邻的 memory 上设置权限,也可能会破坏事情。free
, you might break malloc
's internals.free
之前不恢复旧权限,您可能会破坏malloc
的内部结构。 malloc()
will normally return memory with read and write permissions. malloc()
通常会返回具有读写权限的 memory。 Some architectures (eg: older x86) may not allow disabling execute permission in a straightforward way, but that's just a defficiency of the platform.某些架构(例如:较旧的 x86)可能不允许以直接方式禁用执行权限,但这只是平台的缺陷。
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).如果你想从你分配的 memory 执行代码,你必须明确地给予执行权限,并且可能你必须删除写权限,因为在同一个 memory 上同时拥有写和执行权限在某些系统上被认为是潜在危险的(通常称为 W^X)。
There have been several other threads on executing code from memory allocated by the programmer:还有几个其他线程用于执行程序员分配的 memory 中的代码:
Allocate executable ram in c on linux 在 linux 上的 c 中分配可执行 ram
Is it possible to execute code from the stack in standard C? 是否可以从标准 C 中的堆栈执行代码?
You may need to call mprotect
to set the PROT_EXEC
flag yourself, after the memory has been allocated.在分配 memory 之后,您可能需要调用
mprotect
自己设置PROT_EXEC
标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.