upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / mm / mprotect.c
1 /*
2  *  mm/mprotect.c
3  *
4  *  (C) Copyright 1994 Linus Torvalds
5  *  (C) Copyright 2002 Christoph Hellwig
6  *
7  *  Address space accounting code       <alan@redhat.com>
8  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9  */
10
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/fs.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
21 #include <linux/syscalls.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/pgalloc.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28
29 static inline void
30 change_pte_range(pmd_t *pmd, unsigned long address,
31                 unsigned long size, pgprot_t newprot)
32 {
33         pte_t * pte;
34         unsigned long end;
35
36         if (pmd_none(*pmd))
37                 return;
38         if (pmd_bad(*pmd)) {
39                 pmd_ERROR(*pmd);
40                 pmd_clear(pmd);
41                 return;
42         }
43         pte = pte_offset_map(pmd, address);
44         address &= ~PMD_MASK;
45         end = address + size;
46         if (end > PMD_SIZE)
47                 end = PMD_SIZE;
48         do {
49                 if (pte_present(*pte)) {
50                         pte_t entry;
51
52                         /* Avoid an SMP race with hardware updated dirty/clean
53                          * bits by wiping the pte and then setting the new pte
54                          * into place.
55                          */
56                         entry = ptep_get_and_clear(pte);
57                         set_pte(pte, pte_modify(entry, newprot));
58                 }
59                 address += PAGE_SIZE;
60                 pte++;
61         } while (address && (address < end));
62         pte_unmap(pte - 1);
63 }
64
65 static inline void
66 change_pmd_range(pgd_t *pgd, unsigned long address,
67                 unsigned long size, pgprot_t newprot)
68 {
69         pmd_t * pmd;
70         unsigned long end;
71
72         if (pgd_none(*pgd))
73                 return;
74         if (pgd_bad(*pgd)) {
75                 pgd_ERROR(*pgd);
76                 pgd_clear(pgd);
77                 return;
78         }
79         pmd = pmd_offset(pgd, address);
80         address &= ~PGDIR_MASK;
81         end = address + size;
82         if (end > PGDIR_SIZE)
83                 end = PGDIR_SIZE;
84         do {
85                 change_pte_range(pmd, address, end - address, newprot);
86                 address = (address + PMD_SIZE) & PMD_MASK;
87                 pmd++;
88         } while (address && (address < end));
89 }
90
91 static void
92 change_protection(struct vm_area_struct *vma, unsigned long start,
93                 unsigned long end, pgprot_t newprot)
94 {
95         pgd_t *dir;
96         unsigned long beg = start;
97
98         dir = pgd_offset(current->mm, start);
99         flush_cache_range(vma, beg, end);
100         if (start >= end)
101                 BUG();
102         spin_lock(&current->mm->page_table_lock);
103         do {
104                 change_pmd_range(dir, start, end - start, newprot);
105                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
106                 dir++;
107         } while (start && (start < end));
108         flush_tlb_range(vma, beg, end);
109         spin_unlock(&current->mm->page_table_lock);
110         return;
111 }
112
113 static int
114 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
115         unsigned long start, unsigned long end, unsigned long newflags)
116 {
117         struct mm_struct * mm = vma->vm_mm;
118         unsigned long oldflags = vma->vm_flags;
119         long nrpages = (end - start) >> PAGE_SHIFT;
120         unsigned long charged = 0, old_end = vma->vm_end;
121         pgprot_t newprot;
122         pgoff_t pgoff;
123         int error;
124
125         if (newflags == oldflags) {
126                 *pprev = vma;
127                 return 0;
128         }
129
130         /*
131          * If we make a private mapping writable we increase our commit;
132          * but (without finer accounting) cannot reduce our commit if we
133          * make it unwritable again.
134          *
135          * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
136          * a MAP_NORESERVE private mapping to writable will now reserve.
137          */
138         if (newflags & VM_WRITE) {
139                 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
140                         charged = nrpages;
141                         if (security_vm_enough_memory(charged))
142                                 return -ENOMEM;
143                         newflags |= VM_ACCOUNT;
144                 }
145         }
146
147         newprot = protection_map[newflags & 0xf];
148
149         /*
150          * First try to merge with previous and/or next vma.
151          */
152         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
153         *pprev = vma_merge(mm, *pprev, start, end, newflags,
154                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
155         if (*pprev) {
156                 vma = *pprev;
157                 goto success;
158         }
159
160         if (start != vma->vm_start) {
161                 error = split_vma(mm, vma, start, 1);
162                 if (error)
163                         goto fail;
164         }
165         /*
166          * Unless it returns an error, this function always sets *pprev to
167          * the first vma for which vma->vm_end >= end.
168          */
169         *pprev = vma;
170
171         if (end != vma->vm_end) {
172                 error = split_vma(mm, vma, end, 0);
173                 if (error)
174                         goto fail;
175         }
176
177 success:
178         /*
179          * vm_flags and vm_page_prot are protected by the mmap_sem
180          * held in write mode.
181          */
182         oldflags = vma->vm_flags;
183         vma->vm_flags = newflags;
184         vma->vm_page_prot = newprot;
185         if (oldflags & VM_EXEC)
186                 arch_remove_exec_range(current->mm, old_end);
187         change_protection(vma, start, end, newprot);
188         __vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
189         __vm_stat_account(mm, newflags, vma->vm_file, nrpages);
190         return 0;
191
192 fail:
193         vm_unacct_memory(charged);
194         return error;
195 }
196
197 asmlinkage long
198 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len, 
199              unsigned long prot)
200 {
201         unsigned long vm_flags, nstart, end, tmp;
202         struct vm_area_struct *vma, *prev;
203         int error = -EINVAL;
204         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
205         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
206         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
207                 return -EINVAL;
208
209         if (start & ~PAGE_MASK)
210                 return -EINVAL;
211         len = PAGE_ALIGN(len);
212         end = start + len;
213         if (end < start)
214                 return -ENOMEM;
215         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
216                 return -EINVAL;
217         if (end == start)
218                 return 0;
219         /*
220          * Does the application expect PROT_READ to imply PROT_EXEC:
221          */
222         if (unlikely((prot & PROT_READ) &&
223                         (current->personality & READ_IMPLIES_EXEC)))
224                 prot |= PROT_EXEC;
225
226         vm_flags = calc_vm_prot_bits(prot);
227
228         down_write(&mm->mmap_sem);
229
230         vma = find_vma_prev(mm, start, &prev);
231         error = -ENOMEM;
232         if (!vma)
233                 goto out;
234         if (unlikely(grows & PROT_GROWSDOWN)) {
235                 if (vma->vm_start >= end)
236                         goto out;
237                 start = vma->vm_start;
238                 error = -EINVAL;
239                 if (!(vma->vm_flags & VM_GROWSDOWN))
240                         goto out;
241         }
242         else {
243                 if (vma->vm_start > start)
244                         goto out;
245                 if (unlikely(grows & PROT_GROWSUP)) {
246                         end = vma->vm_end;
247                         error = -EINVAL;
248                         if (!(vma->vm_flags & VM_GROWSUP))
249                                 goto out;
250                 }
251         }
252         if (start > vma->vm_start)
253                 prev = vma;
254
255         for (nstart = start ; ; ) {
256                 unsigned long newflags;
257
258                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
259
260                 if (is_vm_hugetlb_page(vma)) {
261                         error = -EACCES;
262                         goto out;
263                 }
264
265                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
266
267                 if ((newflags & ~(newflags >> 4)) & 0xf) {
268                         error = -EACCES;
269                         goto out;
270                 }
271
272                 error = security_file_mprotect(vma, prot);
273                 if (error)
274                         goto out;
275
276                 tmp = vma->vm_end;
277                 if (tmp > end)
278                         tmp = end;
279                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
280                 if (error)
281                         goto out;
282                 nstart = tmp;
283
284                 if (nstart < prev->vm_end)
285                         nstart = prev->vm_end;
286                 if (nstart >= end)
287                         goto out;
288
289                 vma = prev->vm_next;
290                 if (!vma || vma->vm_start != nstart) {
291                         error = -ENOMEM;
292                         goto out;
293                 }
294         }
295 out:
296         up_write(&mm->mmap_sem);
297         return error;
298 }
299
300 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
301 {
302         return(do_mprotect(current->mm, start, len, prot));
303 }