LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to set write protection to all page in Virtual memory of a process? (https://www.linuxquestions.org/questions/programming-9/how-to-set-write-protection-to-all-page-in-virtual-memory-of-a-process-4175536646/)

helloworld2008 03-13-2015 10:59 AM

How to set write protection to all page in Virtual memory of a process?
 
Hi all,

I want to write a function that set write protection to all page in Virtual memory. I that moment, I do like that:

Code:

mm = task->mm;
spin_lock(&mm->page_table_lock);
 
for (vma = mm->mmap ; vma ; vma = vma->vm_next) {        //loop for all vmas
  vma->vm_flags, (int) vma->vm_flags & VM_WRITE);                                               
  addr = vma->vm_start;
  vma->vm_flags &= ~VM_WRITE;               
  while(addr < vma->vm_end){ //loop for each page
    pgd = pgd_offset(mm, addr);
    if (pgd_none(*pgd)) { addr += PAGE_SIZE; continue; }               
    pud = pud_offset(pgd, addr);
    if (pud_none(*pud)) { addr += PAGE_SIZE; continue; }
    pmd = pmd_offset(pud, addr);
    if (pmd_none(*pmd)) { addr += PAGE_SIZE; continue; }
    pte = pte_offset_map(pmd, addr);
    if (pte_present(*pte)){
        *pte = pte_wrprotect(*pte);
        flush_tlb_page(vma, addr);       
    }
spin_unlock(&mm->page_table_lock); 
...........

My program work in kernel level.

But it just set write protection for a part of memory, it does't set write protection to all page that process is using.

Somebody can help me, please!

KelvinS687 03-27-2015 08:54 AM

Hi,
Can you specify, which type of operating system are you using of Linux? Is it RHEL, Ubuntu, Centos, Fedora etc....

genss 03-27-2015 09:48 AM

@OP memory is in pages, as far as the OS is concerned

@Kelvin try "man mprotect"


All times are GMT -5. The time now is 11:34 PM.