In linux userspace, I know you can use mprotect()
to set the protection of a page, but I've not been able to find anything to retrieve the permissions of a particular page... Does such a thing exist?
Asked
Active
Viewed 52 times
2

gsamaras
- 71,951
- 46
- 188
- 305

HardcoreHenry
- 5,909
- 2
- 19
- 44
-
Check this [answer](https://stackoverflow.com/a/3445113/2411320), where it specifies `/proc/self/maps`. PS: NN, you fast Animal! The answer's section is not here though ;) – gsamaras Jan 22 '19 at 15:46
-
1Yes. Read `/proc/self/maps`. – Nominal Animal Jan 22 '19 at 15:49
-
Just curious - why would it be necessary? In other words, why do you care what current protection level is if you can simply set it the required level? – SergeyA Jan 22 '19 at 15:49
-
@NominalAnimal why did you put an answer in the comment section? – SergeyA Jan 22 '19 at 15:50
-
@SergeyA: Because I vaguely recall already having answered the same question, with example code for parsing it too, but haven't found it yet. – Nominal Animal Jan 22 '19 at 15:52
-
You mean that https://stackoverflow.com/questions/36523584/how-to-see-memory-layout-of-my-program-in-c-during-run-time/36524010#36524010 @NominalAnimal, right? I am closing it as a dupe, if you disagree, please let me know! – gsamaras Jan 22 '19 at 15:54
-
I'm writing a SIGSEGV handler, to count writes to a section of memory in a program. I need to do something to the effect of: `oldPermissions = ?; mprotect(addr,size,oldPermsissions|PROT_WRITE)` ... Unfortuantely, I don't think I can read proc/self/maps in a signal handler... – HardcoreHenry Jan 22 '19 at 16:01
-
@HardcoreHenry: Yes, you can read `/proc/self/maps` in a signal handler using `
` low-level I/O, because there are enough async-signal safe functions to do so, but that will not solve your underlying problem. You see, if your signal handler removes write protection, the code will simply continue to run. Your signal handler will trigger exactly once per page, no more. So, why would the old permissions matter at all? – Nominal Animal Jan 22 '19 at 16:08 -
The handler will also start a timer which will rearm the write protection after 1s. ( If multiple writes occur in that period, it will only count the first one, which is ok for my purposes ). I also want to assert that the page actually has write protection before I disable it, and may need to preserve read protection for the future. – HardcoreHenry Jan 22 '19 at 16:41