Write protected virtual pages, catch write

Is there a way to catch a write to a write protected page?

I am planning on doing a self-similar object system where you copy an object to create it. (because it sounds simple and compact compared to the rest). Obviously, objects created for this purpose must be write-protected in some way. I saw there a way to mark something write protected from program headers in ELF. (RE, RWE, RW -flags)

Does this provide write protection at all? I remember that this should raise the segfault, is it true? How to catch a write to a write protected page. Is this a good way to implement what I want and is there a better way?

0


source to share


1 answer


Yes, you can use mprotect .

Yes, writing to protected memory will increase the segfault. You can install a handler for example. in C ++:

std::signal(SIGSEGV, my_segv_handler_func);

      



This is a believable way of doing what you want, although you have to add a lot of extra steering wheel to get it to work. For example, this kind of write detection is done in hardware, and on x86 architecture you have 4k page size. So you can protect things 4k at a time, aligned to 4k boundaries - not a generic "start at address X and jump N bytes". I believe you will have to either

  • has object-to-page mapping so you can determine whether to write to a page is to write a specific protected object or
  • roll your own malloc that always allocates 4k bounds, which forces you to use the minimum alloc'd 4k block size

I don't know, from my point of view, if there is a better way, but it sounds fun. :)

+1


source







All Articles