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