Page Writable bit not working

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
dc0d32

Page Writable bit not working

Post by dc0d32 »

Hi

i am implementing some memory management stuff. in that, for the user task, i am planning to implement a function which will lock the page/memory area requested by the user task and won't allow any write access [most probably used in the malloc data structure for tyhe appliacation level memory manager]. for that, i am simply making the Writable bit of the given page entry in the page tables as zero. I am also invalidating the TLB. then also, if thru the user task i try to access the page for writing, i expect a PGF, but it doesnt! Instead, if i make the present bit zero, it gives me a PGF, that means the code is correct.

Do i also have to deal with RW bit in the corresponding page directory entry for the page table?

thanx in advance.

[PSB]
AR

Re:Page Writable bit not working

Post by AR »

If the page directory entry has the writable bit turned off then all subpages will automatically be readonly as well.

Are you testing from kernel space or user space? In Ring 0, the CPU ignores the writable bit anyway.
dc0d32

Re:Page Writable bit not working

Post by dc0d32 »

then how to provide readonly memory areas in the kernel itself [like the non wrtitable code and the .rodata sections] so that buggy section of kernel code can be trapped in early stage?
Shecks

Re:Page Writable bit not working

Post by Shecks »

Hi Prashant,

In order for the Read/Write flags to be effective you need to enable the Write Protect bit in CR0. When this bit is 0 all pages are read/write wether their PTE read/write bits are set or not.

See section 4.11 in the Intel System Prog manual for further info. I had the same problem when I was testing my paging code until I found that one out :P

Shecks
dc0d32

Re:Page Writable bit not working

Post by dc0d32 »

It did work finally.

and, for that write protect bit in CR0, AFAIK, it is used for (dis)allowing kernel to write into user pages which are read only in the user's memory area, and not of the kernel.

anyway, thanx
Shecks

Re:Page Writable bit not working

Post by Shecks »

prashant wrote: and, for that write protect bit in CR0, AFAIK, it is used for (dis)allowing kernel to write into user pages which are read only in the user's memory area, and not of the kernel.
But refering to your second question re: kernel mode write protection for kernel code :
prashant wrote: then how to provide readonly memory areas in the kernel itself [like the non wrtitable code and the .rodata sections] so that buggy section of kernel code can be trapped in early stage?
If the WP bit in CR0 is clear then your kernel code will be able to overwrite any pages even if you have marked the pages as read-only.

Shecks
Crazed123

Re:Page Writable bit not working

Post by Crazed123 »

It would be nice if there was a way to make the Write Protect effective on kernel (supervisor, ring 0-2) pages but not user pages.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Page Writable bit not working

Post by Pype.Clicker »

you mean like preventing the OS code to overwrite itself but give it the ability to overwrite pages that are marked 'read-only' at user level ?

There are ways to do that:
- don't enable "kernel write protect" at paging, and enforce read-only things by segmentation.
- create a secondary mapping of the write-protected user pages so that the kernel only can see them (e.g. a kernel-level page directory entry) but has the right to write to them.

The main reason for making the kernel sensitive to user-level "readonly" bit is that you may want to use it for "copy-on-write" purpose and that if the kernel should write to a copy-on-write buffer, it should do the copy too.
Post Reply