views:

77

answers:

2

The mprotect syscall protects the memory area within page boundary:

int mprotect(void *addr, size_t len, int prot);

Here len should be multiple of pagesize.

Is there any way to protect only a few consecutive addresses, which are not aligned to page boundary i.e. len < pagesize ?

+1  A: 

No, there isn't. The virtual memory system of your OS only operates on the page level, nothing smaller than that.

Greg Hewgill
+4  A: 

I wouldn't think so, no. The limitation is because the MMU has hardware limits on how fine a granularity it can control. There are tables that hold the access restrictions and you can't have a table slot for each byte; the table itself would use all your RAM. So instead it's made more coarse, with table entries for each page.

You might be able to do something using Valgrind, if you're on Linux.

unwind