root/mm/mprotect.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. change_pte_range
  2. change_pmd_range
  3. change_protection
  4. mprotect_fixup_all
  5. mprotect_fixup_start
  6. mprotect_fixup_end
  7. mprotect_fixup_middle
  8. mprotect_fixup
  9. sys_mprotect

   1 /*
   2  *      linux/mm/mprotect.c
   3  *
   4  *  (C) Copyright 1994 Linus Torvalds
   5  */
   6 #include <linux/stat.h>
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/mm.h>
  10 #include <linux/shm.h>
  11 #include <linux/errno.h>
  12 #include <linux/mman.h>
  13 #include <linux/string.h>
  14 #include <linux/malloc.h>
  15 
  16 #include <asm/segment.h>
  17 #include <asm/system.h>
  18 #include <asm/pgtable.h>
  19 
  20 static inline void change_pte_range(pmd_t * pmd, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
  21         unsigned long size, pgprot_t newprot)
  22 {
  23         pte_t * pte;
  24         unsigned long end;
  25 
  26         if (pmd_none(*pmd))
  27                 return;
  28         if (pmd_bad(*pmd)) {
  29                 printk("change_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
  30                 pmd_clear(pmd);
  31                 return;
  32         }
  33         pte = pte_offset(pmd, address);
  34         address &= ~PMD_MASK;
  35         end = address + size;
  36         if (end > PMD_SIZE)
  37                 end = PMD_SIZE;
  38         do {
  39                 pte_t entry = *pte;
  40                 if (pte_present(entry))
  41                         set_pte(pte, pte_modify(entry, newprot));
  42                 address += PAGE_SIZE;
  43                 pte++;
  44         } while (address < end);
  45 }
  46 
  47 static inline void change_pmd_range(pgd_t * pgd, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
  48         unsigned long size, pgprot_t newprot)
  49 {
  50         pmd_t * pmd;
  51         unsigned long end;
  52 
  53         if (pgd_none(*pgd))
  54                 return;
  55         if (pgd_bad(*pgd)) {
  56                 printk("change_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
  57                 pgd_clear(pgd);
  58                 return;
  59         }
  60         pmd = pmd_offset(pgd, address);
  61         address &= ~PGDIR_MASK;
  62         end = address + size;
  63         if (end > PGDIR_SIZE)
  64                 end = PGDIR_SIZE;
  65         do {
  66                 change_pte_range(pmd, address, end - address, newprot);
  67                 address = (address + PMD_SIZE) & PMD_MASK;
  68                 pmd++;
  69         } while (address < end);
  70 }
  71 
  72 static void change_protection(unsigned long start, unsigned long end, pgprot_t newprot)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74         pgd_t *dir;
  75         unsigned long beg = start;
  76 
  77         dir = pgd_offset(current->mm, start);
  78         while (start < end) {
  79                 change_pmd_range(dir, start, end - start, newprot);
  80                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
  81                 dir++;
  82         }
  83         invalidate_range(current->mm, beg, end);
  84         return;
  85 }
  86 
  87 static inline int mprotect_fixup_all(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
  88         int newflags, pgprot_t prot)
  89 {
  90         vma->vm_flags = newflags;
  91         vma->vm_page_prot = prot;
  92         return 0;
  93 }
  94 
  95 static inline int mprotect_fixup_start(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
  96         unsigned long end,
  97         int newflags, pgprot_t prot)
  98 {
  99         struct vm_area_struct * n;
 100 
 101         n = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
 102         if (!n)
 103                 return -ENOMEM;
 104         *n = *vma;
 105         vma->vm_start = end;
 106         n->vm_end = end;
 107         vma->vm_offset += vma->vm_start - n->vm_start;
 108         n->vm_flags = newflags;
 109         n->vm_page_prot = prot;
 110         if (n->vm_inode)
 111                 n->vm_inode->i_count++;
 112         if (n->vm_ops && n->vm_ops->open)
 113                 n->vm_ops->open(n);
 114         insert_vm_struct(current, n);
 115         return 0;
 116 }
 117 
 118 static inline int mprotect_fixup_end(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 119         unsigned long start,
 120         int newflags, pgprot_t prot)
 121 {
 122         struct vm_area_struct * n;
 123 
 124         n = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
 125         if (!n)
 126                 return -ENOMEM;
 127         *n = *vma;
 128         vma->vm_end = start;
 129         n->vm_start = start;
 130         n->vm_offset += n->vm_start - vma->vm_start;
 131         n->vm_flags = newflags;
 132         n->vm_page_prot = prot;
 133         if (n->vm_inode)
 134                 n->vm_inode->i_count++;
 135         if (n->vm_ops && n->vm_ops->open)
 136                 n->vm_ops->open(n);
 137         insert_vm_struct(current, n);
 138         return 0;
 139 }
 140 
 141 static inline int mprotect_fixup_middle(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 142         unsigned long start, unsigned long end,
 143         int newflags, pgprot_t prot)
 144 {
 145         struct vm_area_struct * left, * right;
 146 
 147         left = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
 148         if (!left)
 149                 return -ENOMEM;
 150         right = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
 151         if (!right) {
 152                 kfree(left);
 153                 return -ENOMEM;
 154         }
 155         *left = *vma;
 156         *right = *vma;
 157         left->vm_end = start;
 158         vma->vm_start = start;
 159         vma->vm_end = end;
 160         right->vm_start = end;
 161         vma->vm_offset += vma->vm_start - left->vm_start;
 162         right->vm_offset += right->vm_start - left->vm_start;
 163         vma->vm_flags = newflags;
 164         vma->vm_page_prot = prot;
 165         if (vma->vm_inode)
 166                 vma->vm_inode->i_count += 2;
 167         if (vma->vm_ops && vma->vm_ops->open) {
 168                 vma->vm_ops->open(left);
 169                 vma->vm_ops->open(right);
 170         }
 171         insert_vm_struct(current, left);
 172         insert_vm_struct(current, right);
 173         return 0;
 174 }
 175 
 176 static int mprotect_fixup(struct vm_area_struct * vma, 
     /* [previous][next][first][last][top][bottom][index][help] */
 177         unsigned long start, unsigned long end, unsigned int newflags)
 178 {
 179         pgprot_t newprot;
 180         int error;
 181 
 182         if (newflags == vma->vm_flags)
 183                 return 0;
 184         newprot = protection_map[newflags & 0xf];
 185         if (start == vma->vm_start)
 186                 if (end == vma->vm_end)
 187                         error = mprotect_fixup_all(vma, newflags, newprot);
 188                 else
 189                         error = mprotect_fixup_start(vma, end, newflags, newprot);
 190         else if (end == vma->vm_end)
 191                 error = mprotect_fixup_end(vma, start, newflags, newprot);
 192         else
 193                 error = mprotect_fixup_middle(vma, start, end, newflags, newprot);
 194 
 195         if (error)
 196                 return error;
 197 
 198         change_protection(start, end, newprot);
 199         return 0;
 200 }
 201 
 202 asmlinkage int sys_mprotect(unsigned long start, size_t len, unsigned long prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 203 {
 204         unsigned long nstart, end, tmp;
 205         struct vm_area_struct * vma, * next;
 206         int error;
 207 
 208         if (start & ~PAGE_MASK)
 209                 return -EINVAL;
 210         len = (len + ~PAGE_MASK) & PAGE_MASK;
 211         end = start + len;
 212         if (end < start)
 213                 return -EINVAL;
 214         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
 215                 return -EINVAL;
 216         if (end == start)
 217                 return 0;
 218         vma = find_vma(current, start);
 219         if (!vma || vma->vm_start > start)
 220                 return -EFAULT;
 221 
 222         for (nstart = start ; ; ) {
 223                 unsigned int newflags;
 224 
 225                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
 226 
 227                 newflags = prot | (vma->vm_flags & ~(PROT_READ | PROT_WRITE | PROT_EXEC));
 228                 if ((newflags & ~(newflags >> 4)) & 0xf) {
 229                         error = -EACCES;
 230                         break;
 231                 }
 232 
 233                 if (vma->vm_end >= end) {
 234                         error = mprotect_fixup(vma, nstart, end, newflags);
 235                         break;
 236                 }
 237 
 238                 tmp = vma->vm_end;
 239                 next = vma->vm_next;
 240                 error = mprotect_fixup(vma, nstart, tmp, newflags);
 241                 if (error)
 242                         break;
 243                 nstart = tmp;
 244                 vma = next;
 245                 if (!vma || vma->vm_start != nstart) {
 246                         error = -EFAULT;
 247                         break;
 248                 }
 249         }
 250         merge_segments(current, start, end);
 251         return error;
 252 }

/* [previous][next][first][last][top][bottom][index][help] */