Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,otherwise the contaned function can't be executed .

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

like image 476
Je Rog Avatar asked Jun 11 '11 10:06

Je Rog


1 Answers

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.
like image 186
R.. GitHub STOP HELPING ICE Avatar answered Nov 05 '22 02:11

R.. GitHub STOP HELPING ICE