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