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 #include <linux/slab.h>
39 unsigned int *sched_addr;
40 unsigned int *fork_addr;
43 #define GUP_FLAGS_WRITE 0x1
44 #define GUP_FLAGS_FORCE 0x2
45 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
46 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
48 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
49 struct mm_struct* init_mm_ptr;
50 struct mm_struct init_mm;
54 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);
56 #if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 32)
57 DECLARE_MOD_FUNC_DEP(copy_to_user_page, void, struct vm_area_struct *vma, struct page *page, unsigned long uaddr, void *dst, const void *src, unsigned long len);
60 DECLARE_MOD_FUNC_DEP(find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr);
62 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
63 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
64 DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access);
67 DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags);
68 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30) */
70 DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct task_struct *tsk);
72 #ifdef CONFIG_HUGETLB_PAGE
73 DECLARE_MOD_FUNC_DEP(follow_hugetlb_page, int, struct mm_struct *mm, struct vm_area_struct *vma, struct page **pages, struct vm_area_struct **vmas, unsigned long *position, int *length, int i, int write);
76 #ifdef __HAVE_ARCH_GATE_AREA
77 DECLARE_MOD_FUNC_DEP(in_gate_area, int, struct task_struct *tsk,unsigned long addr);
79 DECLARE_MOD_FUNC_DEP(in_gate_area_no_task, int, unsigned long addr);
81 DECLARE_MOD_FUNC_DEP(follow_page, \
82 struct page *, struct vm_area_struct * vma, \
83 unsigned long address, unsigned int foll_flags);
84 DECLARE_MOD_FUNC_DEP(__flush_anon_page, \
85 void, struct vm_area_struct *vma, struct page *page, \
86 unsigned long vmaddr);
87 DECLARE_MOD_FUNC_DEP(vm_normal_page, \
88 struct page *, struct vm_area_struct *vma, \
89 unsigned long addr, pte_t pte);
91 DECLARE_MOD_FUNC_DEP(flush_ptrace_access, \
92 void, struct vm_area_struct *vma, struct page *page, \
93 unsigned long uaddr, void *kaddr, unsigned long len, int write);
96 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
97 DECLARE_MOD_FUNC_DEP(put_task_struct, \
98 void, struct task_struct *tsk);
100 DECLARE_MOD_FUNC_DEP(put_task_struct, \
101 void, struct rcu_head * rhp);
104 DECLARE_MOD_DEP_WRAPPER(access_process_vm, int, struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
105 IMP_MOD_DEP_WRAPPER (access_process_vm, tsk, addr, buf, len, write)
107 DECLARE_MOD_DEP_WRAPPER (find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr)
108 IMP_MOD_DEP_WRAPPER (find_extend_vma, mm, addr)
110 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
111 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
112 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
113 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access)
114 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, write_access)
117 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
118 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags)
119 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, flags)
122 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
123 struct vm_area_struct *, struct task_struct *tsk)
124 IMP_MOD_DEP_WRAPPER (get_gate_vma, tsk)
126 #ifdef CONFIG_HUGETLB_PAGE
127 DECLARE_MOD_DEP_WRAPPER (follow_hugetlb_page, int, struct mm_struct *mm, struct vm_area_struct *vma, struct page **pages, struct vm_area_struct **vmas, unsigned long *position, int *length, int i, int write)
128 IMP_MOD_DEP_WRAPPER (follow_hugetlb_page, mm, vma, pages, vmas, position, length, i, write)
131 #ifdef __HAVE_ARCH_GATE_AREA
132 DECLARE_MOD_DEP_WRAPPER (in_gate_area, int, struct task_struct *tsk, unsigned long addr)
133 IMP_MOD_DEP_WRAPPER (in_gate_area, tsk, addr)
135 DECLARE_MOD_DEP_WRAPPER (in_gate_area_no_task, int, unsigned long addr)
136 IMP_MOD_DEP_WRAPPER (in_gate_area_no_task, addr)
139 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 11))
140 DECLARE_MOD_DEP_WRAPPER (follow_page, \
141 struct page *, struct vm_area_struct * vma, \
142 unsigned long address, unsigned int foll_flags)
143 IMP_MOD_DEP_WRAPPER (follow_page, vma, address, foll_flags)
145 DECLARE_MOD_DEP_WRAPPER (__flush_anon_page, \
146 void, struct vm_area_struct *vma, \
147 struct page *page, unsigned long vmaddr)
148 IMP_MOD_DEP_WRAPPER (__flush_anon_page, vma, page, vmaddr)
150 DECLARE_MOD_DEP_WRAPPER(vm_normal_page, \
151 struct page *, struct vm_area_struct *vma, \
152 unsigned long addr, pte_t pte)
153 IMP_MOD_DEP_WRAPPER (vm_normal_page, vma, addr, pte)
155 DECLARE_MOD_DEP_WRAPPER (flush_ptrace_access, \
156 void, struct vm_area_struct *vma, struct page *page, \
157 unsigned long uaddr, void *kaddr, unsigned long len, int write)
158 IMP_MOD_DEP_WRAPPER (flush_ptrace_access, vma, page, uaddr, kaddr, len, write)
160 #if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 32)
161 DECLARE_MOD_DEP_WRAPPER(copy_to_user_page, void, struct vm_area_struct *vma, struct page *page, unsigned long uaddr, void *dst, const void *src, unsigned long len)
162 IMP_MOD_DEP_WRAPPER (copy_to_user_page, vma, page, uaddr, dst, src, len)
166 int init_module_dependencies()
169 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
170 init_mm_ptr = (struct mm_struct*) kallsyms_search ("init_mm");
171 memcmp(init_mm_ptr, &init_mm, sizeof(struct mm_struct));
174 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
175 INIT_MOD_DEP_VAR(handle_mm_fault, handle_mm_fault);
178 INIT_MOD_DEP_VAR(flush_ptrace_access, flush_ptrace_access);
179 INIT_MOD_DEP_VAR(find_extend_vma, find_extend_vma);
180 INIT_MOD_DEP_VAR(get_gate_vma, get_gate_vma);
182 #ifdef CONFIG_HUGETLB_PAGE
183 INIT_MOD_DEP_VAR(follow_hugetlb_page, follow_hugetlb_page);
186 #ifdef __HAVE_ARCH_GATE_AREA
187 INIT_MOD_DEP_VAR(in_gate_area, in_gate_area);
189 INIT_MOD_DEP_VAR(in_gate_area_no_task, in_gate_area_no_task);
191 INIT_MOD_DEP_VAR(follow_page, follow_page);
193 INIT_MOD_DEP_VAR(__flush_anon_page, __flush_anon_page);
194 INIT_MOD_DEP_VAR(vm_normal_page, vm_normal_page);
195 INIT_MOD_DEP_VAR(access_process_vm, access_process_vm);
197 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
198 # if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11))
199 INIT_MOD_DEP_VAR(put_task_struct, put_task_struct);
201 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct);
204 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct_cb);
208 #if (LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 32))
209 INIT_MOD_DEP_VAR(copy_to_user_page, copy_to_user_page);
215 #define GUP_FLAGS_WRITE 0x1
216 #define GUP_FLAGS_FORCE 0x2
217 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
218 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
220 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
221 static inline int use_zero_page(struct vm_area_struct *vma)
224 * We don't want to optimize FOLL_ANON for make_pages_present()
225 * when it tries to page in a VM_LOCKED region. As to VM_SHARED,
226 * we want to get the page from the page tables to make sure
227 * that we serialize and update with any other user of that
230 if (vma->vm_flags & (VM_LOCKED | VM_SHARED))
233 * And if we have a fault routine, it's not an anonymous region.
235 return !vma->vm_ops || !vma->vm_ops->fault;
238 int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
239 unsigned long start, int len, int flags,
240 struct page **pages, struct vm_area_struct **vmas)
243 unsigned int vm_flags = 0;
244 int write = !!(flags & GUP_FLAGS_WRITE);
245 int force = !!(flags & GUP_FLAGS_FORCE);
246 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
247 int ignore_sigkill = !!(flags & GUP_FLAGS_IGNORE_SIGKILL);
252 * Require read or write permissions.
253 * If 'force' is set, we only require the "MAY" flags.
255 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
256 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
260 struct vm_area_struct *vma;
261 unsigned int foll_flags;
263 //vma = find_extend_vma(mm, start);
264 vma = find_vma(mm, start);
265 if (!vma && in_gate_area(tsk, start)) {
266 unsigned long pg = start & PAGE_MASK;
267 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
273 /* user gate pages are read-only */
274 if (!ignore && write)
275 return i ? : -EFAULT;
277 pgd = pgd_offset_k(pg);
279 pgd = pgd_offset_gate(mm, pg);
280 BUG_ON(pgd_none(*pgd));
281 pud = pud_offset(pgd, pg);
282 BUG_ON(pud_none(*pud));
283 pmd = pmd_offset(pud, pg);
285 return i ? : -EFAULT;
286 pte = pte_offset_map(pmd, pg);
287 if (pte_none(*pte)) {
289 return i ? : -EFAULT;
292 struct page *page = vm_normal_page(gate_vma, start, *pte);
307 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
308 (!ignore && !(vm_flags & vma->vm_flags)))
309 return i ? : -EFAULT;
311 if (is_vm_hugetlb_page(vma)) {
312 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
313 i = follow_hugetlb_page(mm, vma, pages, vmas,
316 i = follow_hugetlb_page(mm, vma, pages, vmas,
317 &start, &len, i, write);
322 foll_flags = FOLL_TOUCH;
324 foll_flags |= FOLL_GET;
326 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
327 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,30)
328 if (!write && use_zero_page(vma))
329 foll_flags |= FOLL_ANON;
338 * If we have a pending SIGKILL, don't keep faulting
339 * pages and potentially allocating memory, unless
340 * current is handling munlock--e.g., on exit. In
341 * that case, we are not allocating memory. Rather,
342 * we're only unlocking already resident/mapped pages.
344 if (unlikely(!ignore_sigkill &&
345 fatal_signal_pending(current)))
346 return i ? i : -ERESTARTSYS;
350 foll_flags |= FOLL_WRITE;
355 DBPRINTF ("pages = %p vma = %p\n", pages, vma);
356 while (!(page = follow_page(vma, start, foll_flags))) {
358 ret = handle_mm_fault(mm, vma, start,
359 foll_flags & FOLL_WRITE);
361 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
362 if (ret & VM_FAULT_WRITE)
363 foll_flags &= ~FOLL_WRITE;
365 switch (ret & ~VM_FAULT_WRITE) {
372 case VM_FAULT_SIGBUS:
373 return i ? i : -EFAULT;
375 return i ? i : -ENOMEM;
381 if (ret & VM_FAULT_ERROR) {
382 if (ret & VM_FAULT_OOM)
383 return i ? i : -ENOMEM;
384 else if (ret & VM_FAULT_SIGBUS)
385 return i ? i : -EFAULT;
388 if (ret & VM_FAULT_MAJOR)
394 * The VM_FAULT_WRITE bit tells us that
395 * do_wp_page has broken COW when necessary,
396 * even if maybe_mkwrite decided not to set
397 * pte_write. We can thus safely do subsequent
398 * page lookups as if they were reads. But only
399 * do so when looping for pte_write is futile:
400 * in some cases userspace may also be wanting
401 * to write to the gotten user page, which a
402 * read fault here might prevent (a readonly
403 * page might get reCOWed by userspace write).
405 if ((ret & VM_FAULT_WRITE) &&
406 !(vma->vm_flags & VM_WRITE))
407 foll_flags &= ~FOLL_WRITE;
415 return i ? i : PTR_ERR(page);
419 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
420 flush_anon_page(page, start);
422 flush_anon_page(vma, page, start);
424 flush_dcache_page(page);
431 } while (len && start < vma->vm_end);
438 int get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
439 unsigned long start, int len, int write, int force,
440 struct page **pages, struct vm_area_struct **vmas)
442 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
446 flags |= GUP_FLAGS_WRITE;
448 flags |= GUP_FLAGS_FORCE;
450 return __get_user_pages_uprobe(tsk, mm,
454 return get_user_pages(tsk, mm, start, len, write, force, pages, vmas);
459 int access_process_vm_atomic(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
461 struct mm_struct *mm;
462 struct vm_area_struct *vma;
464 unsigned long addr1 = addr;
465 unsigned int* inst_buf = (unsigned int*)old_buf;
468 mm = get_task_mm(tsk);
472 down_read(&mm->mmap_sem);
473 /* ignore errors, just check how much was successfully transferred */
475 int bytes, ret, offset;
477 struct page *page = NULL;
479 ret = get_user_pages_uprobe(tsk, mm, addr, 1,
480 write, 1, &page, &vma);
484 * Check if this is a VM_IO | VM_PFNMAP VMA, which
485 * we can access using slightly different code.
487 #ifdef CONFIG_HAVE_IOREMAP_PROT
488 vma = find_vma(mm, addr);
491 if (vma->vm_ops && vma->vm_ops->access)
492 ret = vma->vm_ops->access(vma, addr, buf,
500 offset = addr & (PAGE_SIZE-1);
501 if (bytes > PAGE_SIZE-offset)
502 bytes = PAGE_SIZE-offset;
506 copy_to_user_page(vma, page, addr,
507 maddr + offset, buf, bytes);
508 set_page_dirty_lock(page);
510 copy_from_user_page(vma, page, addr,
511 buf, maddr + offset, bytes);
514 page_cache_release(page);
520 up_read(&mm->mmap_sem);
523 return buf - old_buf;
526 int page_present (struct mm_struct *mm, unsigned long address)
534 pgd = pgd_offset(mm, address);
535 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
538 pud = pud_offset(pgd, address);
539 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
542 pmd = pmd_offset(pud, address);
543 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
546 ptep = pte_offset_map(pmd, address);
552 if (pte_present(pte)) {
554 if (pfn_valid(pfn)) {
564 EXPORT_SYMBOL_GPL (page_present);
565 EXPORT_SYMBOL_GPL (get_user_pages_uprobe);
566 EXPORT_SYMBOL_GPL (access_process_vm_atomic);