This source file includes following definitions.
- change_pte_range
- change_pmd_range
- change_protection
- mprotect_fixup_all
- mprotect_fixup_start
- mprotect_fixup_end
- mprotect_fixup_middle
- mprotect_fixup
- sys_mprotect
1
2
3
4
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,
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 *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,
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)
73 {
74 pgd_t *dir;
75
76 dir = pgd_offset(current, start);
77 while (start < end) {
78 change_pmd_range(dir, start, end - start, newprot);
79 start = (start + PGDIR_SIZE) & PGDIR_MASK;
80 dir++;
81 }
82 invalidate();
83 return;
84 }
85
86 static inline int mprotect_fixup_all(struct vm_area_struct * vma,
87 int newflags, pgprot_t prot)
88 {
89 vma->vm_flags = newflags;
90 vma->vm_page_prot = prot;
91 return 0;
92 }
93
94 static inline int mprotect_fixup_start(struct vm_area_struct * vma,
95 unsigned long end,
96 int newflags, pgprot_t prot)
97 {
98 struct vm_area_struct * n;
99
100 n = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
101 if (!n)
102 return -ENOMEM;
103 *n = *vma;
104 vma->vm_start = end;
105 n->vm_end = end;
106 vma->vm_offset += vma->vm_start - n->vm_start;
107 n->vm_flags = newflags;
108 n->vm_page_prot = prot;
109 if (n->vm_inode)
110 n->vm_inode->i_count++;
111 if (n->vm_ops && n->vm_ops->open)
112 n->vm_ops->open(n);
113 insert_vm_struct(current, n);
114 return 0;
115 }
116
117 static inline int mprotect_fixup_end(struct vm_area_struct * vma,
118 unsigned long start,
119 int newflags, pgprot_t prot)
120 {
121 struct vm_area_struct * n;
122
123 n = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
124 if (!n)
125 return -ENOMEM;
126 *n = *vma;
127 vma->vm_end = start;
128 n->vm_start = start;
129 n->vm_offset += n->vm_start - vma->vm_start;
130 n->vm_flags = newflags;
131 n->vm_page_prot = prot;
132 if (n->vm_inode)
133 n->vm_inode->i_count++;
134 if (n->vm_ops && n->vm_ops->open)
135 n->vm_ops->open(n);
136 insert_vm_struct(current, n);
137 return 0;
138 }
139
140 static inline int mprotect_fixup_middle(struct vm_area_struct * vma,
141 unsigned long start, unsigned long end,
142 int newflags, pgprot_t prot)
143 {
144 struct vm_area_struct * left, * right;
145
146 left = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
147 if (!left)
148 return -ENOMEM;
149 right = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
150 if (!right) {
151 kfree(left);
152 return -ENOMEM;
153 }
154 *left = *vma;
155 *right = *vma;
156 left->vm_end = start;
157 vma->vm_start = start;
158 vma->vm_end = end;
159 right->vm_start = end;
160 vma->vm_offset += vma->vm_start - left->vm_start;
161 right->vm_offset += right->vm_start - left->vm_start;
162 vma->vm_flags = newflags;
163 vma->vm_page_prot = prot;
164 if (vma->vm_inode)
165 vma->vm_inode->i_count += 2;
166 if (vma->vm_ops && vma->vm_ops->open) {
167 vma->vm_ops->open(left);
168 vma->vm_ops->open(right);
169 }
170 insert_vm_struct(current, left);
171 insert_vm_struct(current, right);
172 return 0;
173 }
174
175 static int mprotect_fixup(struct vm_area_struct * vma,
176 unsigned long start, unsigned long end, unsigned int newflags)
177 {
178 pgprot_t newprot;
179 int error;
180
181 if (newflags == vma->vm_flags)
182 return 0;
183 newprot = protection_map[newflags & 0xf];
184 if (start == vma->vm_start)
185 if (end == vma->vm_end)
186 error = mprotect_fixup_all(vma, newflags, newprot);
187 else
188 error = mprotect_fixup_start(vma, end, newflags, newprot);
189 else if (end == vma->vm_end)
190 error = mprotect_fixup_end(vma, start, newflags, newprot);
191 else
192 error = mprotect_fixup_middle(vma, start, end, newflags, newprot);
193
194 if (error)
195 return error;
196
197 change_protection(start, end, newprot);
198 return 0;
199 }
200
201 asmlinkage int sys_mprotect(unsigned long start, size_t len, unsigned long prot)
202 {
203 unsigned long nstart, end, tmp;
204 struct vm_area_struct * vma, * next;
205 int error;
206
207 if (start & ~PAGE_MASK)
208 return -EINVAL;
209 len = (len + ~PAGE_MASK) & PAGE_MASK;
210 end = start + len;
211 if (end < start)
212 return -EINVAL;
213 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
214 return -EINVAL;
215 if (end == start)
216 return 0;
217 vma = find_vma(current, start);
218 if (!vma || vma->vm_start > start)
219 return -EFAULT;
220
221 for (nstart = start ; ; ) {
222 unsigned int newflags;
223
224
225
226 newflags = prot | (vma->vm_flags & ~(PROT_READ | PROT_WRITE | PROT_EXEC));
227 if ((newflags & ~(newflags >> 4)) & 0xf) {
228 error = -EACCES;
229 break;
230 }
231
232 if (vma->vm_end >= end) {
233 error = mprotect_fixup(vma, nstart, end, newflags);
234 break;
235 }
236
237 tmp = vma->vm_end;
238 next = vma->vm_next;
239 error = mprotect_fixup(vma, nstart, tmp, newflags);
240 if (error)
241 break;
242 nstart = tmp;
243 vma = next;
244 if (!vma || vma->vm_start != nstart) {
245 error = -EFAULT;
246 break;
247 }
248 }
249 merge_segments(current, start, end);
250 return error;
251 }