vserver 1.9.5.x5
[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/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(pud_t *pud, unsigned long address,
66                 unsigned long size, pgprot_t newprot)
67 {
68         pmd_t * pmd;
69         unsigned long end;
70
71         if (pud_none(*pud))
72                 return;
73         if (pud_bad(*pud)) {
74                 pud_ERROR(*pud);
75                 pud_clear(pud);
76                 return;
77         }
78         pmd = pmd_offset(pud, address);
79         address &= ~PUD_MASK;
80         end = address + size;
81         if (end > PUD_SIZE)
82                 end = PUD_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 inline void
91 change_pud_range(pgd_t *pgd, unsigned long address,
92                 unsigned long size, pgprot_t newprot)
93 {
94         pud_t * pud;
95         unsigned long end;
96
97         if (pgd_none(*pgd))
98                 return;
99         if (pgd_bad(*pgd)) {
100                 pgd_ERROR(*pgd);
101                 pgd_clear(pgd);
102                 return;
103         }
104         pud = pud_offset(pgd, address);
105         address &= ~PGDIR_MASK;
106         end = address + size;
107         if (end > PGDIR_SIZE)
108                 end = PGDIR_SIZE;
109         do {
110                 change_pmd_range(pud, address, end - address, newprot);
111                 address = (address + PUD_SIZE) & PUD_MASK;
112                 pud++;
113         } while (address && (address < end));
114 }
115
116 static void
117 change_protection(struct vm_area_struct *vma, unsigned long start,
118                 unsigned long end, pgprot_t newprot)
119 {
120         struct mm_struct *mm = current->mm;
121         pgd_t *pgd;
122         unsigned long beg = start, next;
123         int i;
124
125         pgd = pgd_offset(mm, start);
126         flush_cache_range(vma, beg, end);
127         BUG_ON(start >= end);
128         spin_lock(&mm->page_table_lock);
129         for (i = pgd_index(start); i <= pgd_index(end-1); i++) {
130                 next = (start + PGDIR_SIZE) & PGDIR_MASK;
131                 if (next <= start || next > end)
132                         next = end;
133                 change_pud_range(pgd, start, next - start, newprot);
134                 start = next;
135                 pgd++;
136         }
137         flush_tlb_range(vma, beg, end);
138         spin_unlock(&mm->page_table_lock);
139 }
140
141 static int
142 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
143         unsigned long start, unsigned long end, unsigned long newflags)
144 {
145         struct mm_struct * mm = vma->vm_mm;
146         unsigned long oldflags = vma->vm_flags;
147         long nrpages = (end - start) >> PAGE_SHIFT;
148         unsigned long charged = 0;
149         pgprot_t newprot;
150         pgoff_t pgoff;
151         int error;
152
153         if (newflags == oldflags) {
154                 *pprev = vma;
155                 return 0;
156         }
157
158         /*
159          * If we make a private mapping writable we increase our commit;
160          * but (without finer accounting) cannot reduce our commit if we
161          * make it unwritable again.
162          *
163          * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
164          * a MAP_NORESERVE private mapping to writable will now reserve.
165          */
166         if (newflags & VM_WRITE) {
167                 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
168                         charged = nrpages;
169                         if (security_vm_enough_memory(charged))
170                                 return -ENOMEM;
171                         newflags |= VM_ACCOUNT;
172                 }
173         }
174
175         newprot = protection_map[newflags & 0xf];
176
177         /*
178          * First try to merge with previous and/or next vma.
179          */
180         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
181         *pprev = vma_merge(mm, *pprev, start, end, newflags,
182                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
183         if (*pprev) {
184                 vma = *pprev;
185                 goto success;
186         }
187
188         if (start != vma->vm_start) {
189                 error = split_vma(mm, vma, start, 1);
190                 if (error)
191                         goto fail;
192         }
193         /*
194          * Unless it returns an error, this function always sets *pprev to
195          * the first vma for which vma->vm_end >= end.
196          */
197         *pprev = vma;
198
199         if (end != vma->vm_end) {
200                 error = split_vma(mm, vma, end, 0);
201                 if (error)
202                         goto fail;
203         }
204
205 success:
206         /*
207          * vm_flags and vm_page_prot are protected by the mmap_sem
208          * held in write mode.
209          */
210         vma->vm_flags = newflags;
211         vma->vm_page_prot = newprot;
212         change_protection(vma, start, end, newprot);
213         __vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
214         __vm_stat_account(mm, newflags, vma->vm_file, nrpages);
215         return 0;
216
217 fail:
218         vm_unacct_memory(charged);
219         return error;
220 }
221
222 asmlinkage long
223 sys_mprotect(unsigned long start, size_t len, unsigned long prot)
224 {
225         unsigned long vm_flags, nstart, end, tmp;
226         struct vm_area_struct *vma, *prev;
227         int error = -EINVAL;
228         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
229         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
230         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
231                 return -EINVAL;
232
233         if (start & ~PAGE_MASK)
234                 return -EINVAL;
235         len = PAGE_ALIGN(len);
236         end = start + len;
237         if (end < start)
238                 return -ENOMEM;
239         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
240                 return -EINVAL;
241         if (end == start)
242                 return 0;
243         /*
244          * Does the application expect PROT_READ to imply PROT_EXEC:
245          */
246         if (unlikely((prot & PROT_READ) &&
247                         (current->personality & READ_IMPLIES_EXEC)))
248                 prot |= PROT_EXEC;
249
250         vm_flags = calc_vm_prot_bits(prot);
251
252         down_write(&current->mm->mmap_sem);
253
254         vma = find_vma_prev(current->mm, start, &prev);
255         error = -ENOMEM;
256         if (!vma)
257                 goto out;
258         if (unlikely(grows & PROT_GROWSDOWN)) {
259                 if (vma->vm_start >= end)
260                         goto out;
261                 start = vma->vm_start;
262                 error = -EINVAL;
263                 if (!(vma->vm_flags & VM_GROWSDOWN))
264                         goto out;
265         }
266         else {
267                 if (vma->vm_start > start)
268                         goto out;
269                 if (unlikely(grows & PROT_GROWSUP)) {
270                         end = vma->vm_end;
271                         error = -EINVAL;
272                         if (!(vma->vm_flags & VM_GROWSUP))
273                                 goto out;
274                 }
275         }
276         if (start > vma->vm_start)
277                 prev = vma;
278
279         for (nstart = start ; ; ) {
280                 unsigned long newflags;
281
282                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
283
284                 if (is_vm_hugetlb_page(vma)) {
285                         error = -EACCES;
286                         goto out;
287                 }
288
289                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
290
291                 if ((newflags & ~(newflags >> 4)) & 0xf) {
292                         error = -EACCES;
293                         goto out;
294                 }
295
296                 error = security_file_mprotect(vma, prot);
297                 if (error)
298                         goto out;
299
300                 tmp = vma->vm_end;
301                 if (tmp > end)
302                         tmp = end;
303                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
304                 if (error)
305                         goto out;
306                 nstart = tmp;
307
308                 if (nstart < prev->vm_end)
309                         nstart = prev->vm_end;
310                 if (nstart >= end)
311                         goto out;
312
313                 vma = prev->vm_next;
314                 if (!vma || vma->vm_start != nstart) {
315                         error = -ENOMEM;
316                         goto out;
317                 }
318         }
319 out:
320         up_write(&current->mm->mmap_sem);
321         return error;
322 }