azizulhakim
azizulhakim

Reputation: 658

Protect the whole address space using mprotect

For my university project I need to WRITE protect the whole address space of the process. I was reading the /proc/self/maps file and parsing the mapping.

So for each entry of the format 08048000-0804c000 r-xp 00000000 08:03 7971106 /bin/cat, I am reading the first two entry(here 08048000 & 0804c000), converting them to decimal. Lets assume the decimal equivalent is A & B respectively. Then I do mprotect((int*)A, B-A, PROT_READ). But this approach is giving me segmentation fault. I can't find out what I did wrong here. May be I've some knowledge gap here which is causing the problem. Someone can give me some suggestions?

Upvotes: 1

Views: 1114

Answers (1)

Kristof Provost
Kristof Provost

Reputation: 26322

Assuming that your implementation is correct, I'd still expect to see segmentation faults.

After all, you're telling the kernel that you don't want to be allowed to write to any part of your memory. Afterwards, you'll just continue to run your process and the next time you try to write anything at all you'll get a segmentation fault because that's no longer allowed.

That'll most likely be when you return from mprotect() after "protecting" the stack.

Thinking a bit more, it's even possible that you're getting segmentation faults while executing memory (i.e. a shared lib, or your executable code) after you've "protected" it.

In fact, all of the bits of memory where it's safe to apply read-only / do-not-execute flags already have those flags set.

I suspect that's the insight this univerity project was meant to give you.

Upvotes: 1

Related Questions