2 * Dynamic Binary Instrumentation Module based on KProbes
3 * modules/kprobe/dbi_kprobes_deps.h
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) Samsung Electronics, 2006-2010
21 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
22 * Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
23 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
28 #include <linux/module.h>
29 #include <linux/sched.h>
31 #include <asm/pgtable.h>
33 #include "dbi_kprobes_deps.h"
34 #include "dbi_kdebug.h"
37 unsigned int *sched_addr;
38 unsigned int *fork_addr;
41 #define GUP_FLAGS_WRITE 0x1
42 #define GUP_FLAGS_WRITE 0x1
43 #define GUP_FLAGS_FORCE 0x2
44 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
45 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
47 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
48 struct mm_struct* init_mm_ptr;
49 struct mm_struct init_mm;
53 DECLARE_MOD_CB_DEP(kallsyms_search, unsigned long, const char *name);
55 DECLARE_MOD_FUNC_DEP(access_process_vm, int, struct task_struct * tsk, unsigned long addr, void *buf, int len, int write);
57 DECLARE_MOD_FUNC_DEP(find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr);
59 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
60 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
61 DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access);
64 DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags);
65 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30) */
67 DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct task_struct *tsk);
68 DECLARE_MOD_FUNC_DEP(in_gate_area_no_task, int, unsigned long addr);
69 DECLARE_MOD_FUNC_DEP(follow_page, \
70 struct page *, struct vm_area_struct * vma, \
71 unsigned long address, unsigned int foll_flags);
72 DECLARE_MOD_FUNC_DEP(__flush_anon_page, \
73 void, struct vm_area_struct *vma, struct page *page, \
74 unsigned long vmaddr);
75 DECLARE_MOD_FUNC_DEP(vm_normal_page, \
76 struct page *, struct vm_area_struct *vma, \
77 unsigned long addr, pte_t pte);
79 DECLARE_MOD_FUNC_DEP(flush_ptrace_access, \
80 void, struct vm_area_struct *vma, struct page *page, \
81 unsigned long uaddr, void *kaddr, unsigned long len, int write);
84 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
85 DECLARE_MOD_FUNC_DEP(put_task_struct, \
86 void, struct task_struct *tsk);
88 DECLARE_MOD_FUNC_DEP(put_task_struct, \
89 void, struct rcu_head * rhp);
92 DECLARE_MOD_DEP_WRAPPER(access_process_vm, int, struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
93 IMP_MOD_DEP_WRAPPER (access_process_vm, tsk, addr, buf, len, write)
95 DECLARE_MOD_DEP_WRAPPER (find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr)
96 IMP_MOD_DEP_WRAPPER (find_extend_vma, mm, addr)
98 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
99 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
100 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
101 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access)
102 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, write_access)
105 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
106 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags)
107 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, flags)
110 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
111 struct vm_area_struct *, struct task_struct *tsk)
112 IMP_MOD_DEP_WRAPPER (get_gate_vma, tsk)
114 DECLARE_MOD_DEP_WRAPPER (in_gate_area_no_task, int, unsigned long addr)
115 IMP_MOD_DEP_WRAPPER (in_gate_area_no_task, addr)
117 DECLARE_MOD_DEP_WRAPPER (follow_page, \
118 struct page *, struct vm_area_struct * vma, \
119 unsigned long address, unsigned int foll_flags)
120 IMP_MOD_DEP_WRAPPER (follow_page, vma, address, foll_flags)
122 DECLARE_MOD_DEP_WRAPPER (__flush_anon_page, \
123 void, struct vm_area_struct *vma, \
124 struct page *page, unsigned long vmaddr)
125 IMP_MOD_DEP_WRAPPER (__flush_anon_page, vma, page, vmaddr)
127 DECLARE_MOD_DEP_WRAPPER(vm_normal_page, \
128 struct page *, struct vm_area_struct *vma, \
129 unsigned long addr, pte_t pte)
130 IMP_MOD_DEP_WRAPPER (vm_normal_page, vma, addr, pte)
132 DECLARE_MOD_DEP_WRAPPER (flush_ptrace_access, \
133 void, struct vm_area_struct *vma, struct page *page, \
134 unsigned long uaddr, void *kaddr, unsigned long len, int write)
135 IMP_MOD_DEP_WRAPPER (flush_ptrace_access, vma, page, uaddr, kaddr, len, write)
137 int init_module_dependencies()
140 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
141 init_mm_ptr = (struct mm_struct*) kallsyms_search ("init_mm");
142 memcmp(init_mm_ptr, &init_mm, sizeof(struct mm_struct));
145 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
146 INIT_MOD_DEP_VAR(handle_mm_fault, handle_mm_fault);
149 INIT_MOD_DEP_VAR(flush_ptrace_access, flush_ptrace_access);
150 INIT_MOD_DEP_VAR(find_extend_vma, find_extend_vma);
151 INIT_MOD_DEP_VAR(get_gate_vma, get_gate_vma);
152 INIT_MOD_DEP_VAR(in_gate_area_no_task, in_gate_area_no_task);
153 INIT_MOD_DEP_VAR(follow_page, follow_page);
154 INIT_MOD_DEP_VAR(__flush_anon_page, __flush_anon_page);
155 INIT_MOD_DEP_VAR(vm_normal_page, vm_normal_page);
156 INIT_MOD_DEP_VAR(access_process_vm, access_process_vm);
158 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
159 # if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11))
160 INIT_MOD_DEP_VAR(put_task_struct, put_task_struct);
162 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct);
165 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct_cb);
171 #define GUP_FLAGS_WRITE 0x1
172 #define GUP_FLAGS_FORCE 0x2
173 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
174 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
176 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
177 static inline int use_zero_page(struct vm_area_struct *vma)
180 * We don't want to optimize FOLL_ANON for make_pages_present()
181 * when it tries to page in a VM_LOCKED region. As to VM_SHARED,
182 * we want to get the page from the page tables to make sure
183 * that we serialize and update with any other user of that
186 if (vma->vm_flags & (VM_LOCKED | VM_SHARED))
189 * And if we have a fault routine, it's not an anonymous region.
191 return !vma->vm_ops || !vma->vm_ops->fault;
194 int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
195 unsigned long start, int len, int flags,
196 struct page **pages, struct vm_area_struct **vmas)
199 unsigned int vm_flags = 0;
200 int write = !!(flags & GUP_FLAGS_WRITE);
201 int force = !!(flags & GUP_FLAGS_FORCE);
202 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
203 int ignore_sigkill = !!(flags & GUP_FLAGS_IGNORE_SIGKILL);
208 * Require read or write permissions.
209 * If 'force' is set, we only require the "MAY" flags.
211 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
212 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
216 struct vm_area_struct *vma;
217 unsigned int foll_flags;
219 //vma = find_extend_vma(mm, start);
220 vma = find_vma(mm, start);
221 if (!vma && in_gate_area(tsk, start)) {
222 unsigned long pg = start & PAGE_MASK;
223 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
229 /* user gate pages are read-only */
230 if (!ignore && write)
231 return i ? : -EFAULT;
233 pgd = pgd_offset_k(pg);
235 pgd = pgd_offset_gate(mm, pg);
236 BUG_ON(pgd_none(*pgd));
237 pud = pud_offset(pgd, pg);
238 BUG_ON(pud_none(*pud));
239 pmd = pmd_offset(pud, pg);
241 return i ? : -EFAULT;
242 pte = pte_offset_map(pmd, pg);
243 if (pte_none(*pte)) {
245 return i ? : -EFAULT;
248 struct page *page = vm_normal_page(gate_vma, start, *pte);
263 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
264 (!ignore && !(vm_flags & vma->vm_flags)))
265 return i ? : -EFAULT;
267 if (is_vm_hugetlb_page(vma)) {
268 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
269 i = follow_hugetlb_page(mm, vma, pages, vmas,
272 i = follow_hugetlb_page(mm, vma, pages, vmas,
273 &start, &len, i, write);
278 foll_flags = FOLL_TOUCH;
280 foll_flags |= FOLL_GET;
282 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
283 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,30)
284 if (!write && use_zero_page(vma))
285 foll_flags |= FOLL_ANON;
294 * If we have a pending SIGKILL, don't keep faulting
295 * pages and potentially allocating memory, unless
296 * current is handling munlock--e.g., on exit. In
297 * that case, we are not allocating memory. Rather,
298 * we're only unlocking already resident/mapped pages.
300 if (unlikely(!ignore_sigkill &&
301 fatal_signal_pending(current)))
302 return i ? i : -ERESTARTSYS;
306 foll_flags |= FOLL_WRITE;
311 DBPRINTF ("pages = %p vma = %p\n", pages, vma);
312 while (!(page = follow_page(vma, start, foll_flags))) {
314 ret = handle_mm_fault(mm, vma, start,
315 foll_flags & FOLL_WRITE);
317 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
318 if (ret & VM_FAULT_WRITE)
319 foll_flags &= ~FOLL_WRITE;
321 switch (ret & ~VM_FAULT_WRITE) {
328 case VM_FAULT_SIGBUS:
329 return i ? i : -EFAULT;
331 return i ? i : -ENOMEM;
337 if (ret & VM_FAULT_ERROR) {
338 if (ret & VM_FAULT_OOM)
339 return i ? i : -ENOMEM;
340 else if (ret & VM_FAULT_SIGBUS)
341 return i ? i : -EFAULT;
344 if (ret & VM_FAULT_MAJOR)
350 * The VM_FAULT_WRITE bit tells us that
351 * do_wp_page has broken COW when necessary,
352 * even if maybe_mkwrite decided not to set
353 * pte_write. We can thus safely do subsequent
354 * page lookups as if they were reads. But only
355 * do so when looping for pte_write is futile:
356 * in some cases userspace may also be wanting
357 * to write to the gotten user page, which a
358 * read fault here might prevent (a readonly
359 * page might get reCOWed by userspace write).
361 if ((ret & VM_FAULT_WRITE) &&
362 !(vma->vm_flags & VM_WRITE))
363 foll_flags &= ~FOLL_WRITE;
371 return i ? i : PTR_ERR(page);
375 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
376 flush_anon_page(page, start);
378 flush_anon_page(vma, page, start);
380 flush_dcache_page(page);
387 } while (len && start < vma->vm_end);
394 int get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
395 unsigned long start, int len, int write, int force,
396 struct page **pages, struct vm_area_struct **vmas)
398 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
402 flags |= GUP_FLAGS_WRITE;
404 flags |= GUP_FLAGS_FORCE;
406 return __get_user_pages_uprobe(tsk, mm,
410 return get_user_pages(tsk, mm, start, len, write, force, pages, vmas);
414 int access_process_vm_atomic(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
416 struct mm_struct *mm;
417 struct vm_area_struct *vma;
420 mm = get_task_mm(tsk);
424 //down_read(&mm->mmap_sem);
425 /* ignore errors, just check how much was successfully transferred */
427 int bytes, ret, offset;
429 struct page *page = NULL;
431 ret = get_user_pages_uprobe(tsk, mm, addr, 1,
432 write, 1, &page, &vma);
435 * Check if this is a VM_IO | VM_PFNMAP VMA, which
436 * we can access using slightly different code.
438 #ifdef CONFIG_HAVE_IOREMAP_PROT
439 vma = find_vma(mm, addr);
442 if (vma->vm_ops && vma->vm_ops->access)
443 ret = vma->vm_ops->access(vma, addr, buf,
451 offset = addr & (PAGE_SIZE-1);
452 if (bytes > PAGE_SIZE-offset)
453 bytes = PAGE_SIZE-offset;
457 copy_to_user_page(vma, page, addr,
458 maddr + offset, buf, bytes);
459 set_page_dirty_lock(page);
461 copy_from_user_page(vma, page, addr,
462 buf, maddr + offset, bytes);
465 page_cache_release(page);
471 //up_read(&mm->mmap_sem);
474 return buf - old_buf;
477 int page_present (struct mm_struct *mm, unsigned long address)
485 pgd = pgd_offset(mm, address);
486 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
489 pud = pud_offset(pgd, address);
490 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
493 pmd = pmd_offset(pud, address);
494 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
497 ptep = pte_offset_map(pmd, address);
503 if (pte_present(pte)) {
505 if (pfn_valid(pfn)) {
515 EXPORT_SYMBOL_GPL (page_present);
516 EXPORT_SYMBOL_GPL (get_user_pages_uprobe);
517 EXPORT_SYMBOL_GPL (access_process_vm_atomic);