Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
target_os_linux_generic.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __TARGET_OS_LINUX_GENERIC_H__
20 #define __TARGET_OS_LINUX_GENERIC_H__
21 
22 #ifndef PAGE_SIZE
23 #define PAGE_SIZE 0x1000
24 #endif
25 #define THREAD_SIZE 8192
26 /* x86_64 constant used in current_thread_ptr */
27 #define KERNEL_STACK_OFFSET (5*8)
28 
29 #define THREAD_INFO_GET_CPL(tid) (((tid) & (0x3 << 62)) >> 62)
30 #define THREAD_INFO_GET_TID(tid) ((tid) & 0xffffffff)
31 #define THREAD_INFO_SET_CPL(tid,cpl) (tid) |= (((cpl) & 0x3) << 62)
32 #define THREAD_INFO_SET_TID(tid,pid) (tid) |= (0xffffffff & (pid))
33 
34 #define TIF_32_SYSCALL_TRACE 0 /* syscall trace active */
35 #define TIF_32_NOTIFY_RESUME 1 /* resumption notification requested */
36 #define TIF_32_SIGPENDING 2 /* signal pending */
37 #define TIF_32_NEED_RESCHED 3 /* rescheduling necessary */
38 #define TIF_32_SINGLESTEP 4 /* restore singlestep on return to user mode */
39 #define TIF_32_IRET 5 /* return with iret */
40 #define TIF_32_SYSCALL_EMU 6 /* syscall emulation active */
41 #define TIF_32_SYSCALL_AUDIT 7 /* syscall auditing active */
42 #define TIF_32_SECCOMP 8 /* secure computing */
43 #define TIF_32_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
44 #define TIF_32_MEMDIE 16
45 #define TIF_32_DEBUG 17 /* uses debug registers */
46 #define TIF_32_IO_BITMAP 18 /* uses I/O bitmap */
47 
48 #define _TIF_32_SYSCALL_TRACE (1<<TIF_32_SYSCALL_TRACE)
49 #define _TIF_32_NOTIFY_RESUME (1<<TIF_32_NOTIFY_RESUME)
50 #define _TIF_32_SIGPENDING (1<<TIF_32_SIGPENDING)
51 #define _TIF_32_NEED_RESCHED (1<<TIF_32_NEED_RESCHED)
52 #define _TIF_32_SINGLESTEP (1<<TIF_32_SINGLESTEP)
53 #define _TIF_32_IRET (1<<TIF_32_IRET)
54 #define _TIF_32_SYSCALL_EMU (1<<TIF_32_SYSCALL_EMU)
55 #define _TIF_32_SYSCALL_AUDIT (1<<TIF_32_SYSCALL_AUDIT)
56 #define _TIF_32_SECCOMP (1<<TIF_32_SECCOMP)
57 #define _TIF_32_RESTORE_SIGMASK (1<<TIF_32_RESTORE_SIGMASK)
58 #define _TIF_32_DEBUG (1<<TIF_32_DEBUG)
59 #define _TIF_32_IO_BITMAP (1<<TIF_32_IO_BITMAP)
60 
61 
62 #define TIF_64_SYSCALL_TRACE 0 /* syscall trace active */
63 #define TIF_64_NOTIFY_RESUME 1 /* resumption notification requested */
64 #define TIF_64_SIGPENDING 2 /* signal pending */
65 #define TIF_64_NEED_RESCHED 3 /* rescheduling necessary */
66 #define TIF_64_SINGLESTEP 4 /* reenable singlestep on user return*/
67 #define TIF_64_IRET 5 /* force IRET */
68 #define TIF_64_SYSCALL_AUDIT 7 /* syscall auditing active */
69 #define TIF_64_SECCOMP 8 /* secure computing */
70 /* 16 free */
71 #define TIF_64_IA32 17 /* 32bit process */
72 #define TIF_64_FORK 18 /* ret_from_fork */
73 #define TIF_64_ABI_PENDING 19
74 #define TIF_64_MEMDIE 20
75 
76 #define _TIF_64_SYSCALL_TRACE (1<<TIF_64_SYSCALL_TRACE)
77 #define _TIF_64_NOTIFY_RESUME (1<<TIF_64_NOTIFY_RESUME)
78 #define _TIF_64_SIGPENDING (1<<TIF_64_SIGPENDING)
79 #define _TIF_64_SINGLESTEP (1<<TIF_64_SINGLESTEP)
80 #define _TIF_64_NEED_RESCHED (1<<TIF_64_NEED_RESCHED)
81 #define _TIF_64_IRET (1<<TIF_64_IRET)
82 #define _TIF_64_SYSCALL_AUDIT (1<<TIF_64_SYSCALL_AUDIT)
83 #define _TIF_64_SECCOMP (1<<TIF_64_SECCOMP)
84 #define _TIF_64_IA32 (1<<TIF_64_IA32)
85 #define _TIF_64_FORK (1<<TIF_64_FORK)
86 #define _TIF_64_ABI_PENDING (1<<TIF_64_ABI_PENDING)
87 
89  /*
90  * Some kernel task_structs have thread_info; others have void *stack.
91  * Some kernel pt_regs structs have ds/es/fs/gs, or various combinations.
92  * Some kernel thread_structs have debugreg[8];
93  * debugreg0--debugreg7; or a mix of ptrace_bps[4] and debugreg6 and
94  * ptrace_dr7 ... argh.
95  */
106 
107  unsigned int last_thread_count;
109 
110  /*
111  * Some kernel thread_structs have esp/esp0 (older); others have
112  * sp/sp0 (newer). These values are either esp0/sp0/eip, or esp/sp/ip.
113  */
117 
118  /*
119  * On x86_64 systems, figuring out the saved IP for a sleeping
120  * thread is difficult, because the kernel's context switch
121  * functions are written so that they swap context by swapping the
122  * old and new %rsp. See the (much) more detailed comments in
123  * target_os_linux_generic.c ...
124  */
128 
129  /*
130  * Newer kernels store uid/gid info in task->[real_]cred->(uid|gid);
131  * older ones just in task->(uid|gid).
132  */
133  const char *task_uid_member_name;
134  const char *task_gid_member_name;
135 
143 
144  /*
145  * On x86_64, current_thread_ptr is determined by looking at this
146  * per_cpu offset. On x86_64, percpu data is reached via %gs :(.
147  */
150 
162  struct bsymbol *modules;
163 
170  GHashTable *moddep;
172 
176 
180 
182 
183  struct probe *int3_probe;
185 
186  /*
187  * OS Process metadata.
188  */
189 
190  /*
191  * One mm_struct per vma list. These vma caches may be pointed
192  */
193  GHashTable *mm_addr_to_mm_cache;
194 
195  /*
196  * tid_t to struct target_os_process *.
197  */
198  GHashTable *processes;
199 
200  /* These are for APF_PROCESS_MEMORY. */
208 };
209 
210 /*
211  * This just helps us scan for updates to a task's
212  * task_struct->mm->mmap (vm_area_struct list).
213  *
214  * There is one of these for each target memrange.
215  */
216 struct os_linux_vma {
217  struct value *vma;
220  struct memrange *range;
221 };
222 
223 struct os_linux_mm {
224  uint8_t valid:1;
225 
226  struct addrspace *space;
227 
228  struct value *mm;
229  /* Cache these to determine if range is heap/stack. */
233 
235  int vma_len;
236 };
237 
240 
241  /*
242  * This state all comes from the Linux PCB. It is always blown away
243  * on target_resume or target_singlestep .
244  */
245  /* The task struct is always valid unless we are in interrupt
246  * context.
247  */
248  /* @task_struct is a "live" value! it may be value_refresh()'d! */
251  /* The thread_info is always at the bottom of the kernel stack. */
254  num_t thread_info_preempt_count; /* Read-only; not flushed */
255  /* The thread struct comes out of the task struct. */
259  /*
260  * NB: pgd (cr3) is a little funny. If the target is PAE, it might
261  * be > 2**32. So, this value has to always be a u64.
262  *
263  * Also note: this value is always a kernel virtual address; so use
264  * __xen_vm_cr3 to read this value as a physical address.
265  */
266  uint64_t pgd;
268 
269  /*
270  * These are information about the task's kernel stack. esp0 is the
271  * ring 0 stack pointer; stack_base is the bottom of the stack.
272  */
275 
276  /*
277  * These are all for kernel threads, specifically. The only time a
278  * kernel thread will have saved context info is when it has been
279  * preempted or interrupted. Otherwise, the kernel thread has been
280  * context-switched out of, and this does not save its current
281  * register set; context switching only saves esp/eip, fs/gs in the
282  * task's thread struct; eflags and ebp were pushed on the stack
283  * before context switch.
284  */
287  uint16_t fs;
288  uint16_t gs;
289  uint32_t eflags;
291 
292  /*
293  * We only use this for loading process's regions.
294  */
296 };
297 
302  REGVAL kernel_esp);
304  struct symbol *datatype,
305  REGVAL kernel_esp);
306 int os_linux_get_task_pid(struct target *target,struct value *task);
307 int os_linux_get_task_tid(struct target *target,struct value *task);
308 struct value *os_linux_get_task(struct target *target,tid_t tid);
309 
311  struct symbol *datatype,
312  REGVAL kernel_esp);
313 
314 char *os_linux_file_get_path(struct target *target,struct value *task,
315  struct value *file,char *buf,int buflen);
316 
317 #define PREEMPT_MASK 0x000000ff
318 #define SOFTIRQ_MASK 0x0000ff00
319 #define HARDIRQ_MASK 0x0fff0000
320 #define PREEMPT_ACTIVE 0x10000000
321 #define PREEMPT_BITSHIFT 0
322 #define SOFTIRQ_BITSHIFT 8
323 #define HARDIRQ_BITSHIFT 16
324 
325 /*
326  * These macros are different than the kernel's!
327  */
328 #define PREEMPT_COUNT(p) (((p) & PREEMPT_MASK) >> PREEMPT_BITSHIFT)
329 #define SOFTIRQ_COUNT(p) (((p) & SOFTIRQ_MASK) >> SOFTIRQ_BITSHIFT)
330 #define HARDIRQ_COUNT(p) (((p) & HARDIRQ_MASK) >> HARDIRQ_BITSHIFT)
331 
333 
334 /*
335  * If the iterator returns 1, we break out of the loop.
336  * If the iterator returns -1, we break out of the loop, AND do NOT free
337  * @value (so the caller can save it).
338  */
339 typedef int (*os_linux_list_iterator_t)(struct target *t,struct value *value,
340  void *data);
342  char *list_head_member_name,int nofree,
343  os_linux_list_iterator_t iterator,void *data);
344 
345 int os_linux_list_for_each_entry(struct target *t,struct bsymbol *btype,
346  struct bsymbol *list_head,
347  char *list_head_member_name,int nofree,
348  os_linux_list_iterator_t iterator,void *data);
349 
350 #endif /* __TARGET_OS_LINUX_GENERIC_H__ */
unsigned int thread_struct_has_debugreg
struct bsymbol * module_type
struct symbol * task_struct_type
struct symbol * mm_struct_type
unsigned int thread_struct_has_fs
int32_t tid_t
Definition: common.h:36
char * os_linux_file_get_path(struct target *target, struct value *task, struct value *file, char *ibuf, int buflen)
struct probe * active_memory_probe_mremap
struct symbol * os_linux_get_task_struct_type(struct target *target)
struct probe * int3_probe
unsigned int hypervisor_ignores_userspace_exceptions
GHashTable * task_struct_addr_to_thread
struct symbol * pt_regs_type
const char * task_uid_member_name
struct bsymbol * thread_entry_f_symbol
const char * thread_sp0_member_name
struct value * os_linux_load_current_task_as_type(struct target *target, struct symbol *datatype, REGVAL kernel_esp)
int64_t num_t
Definition: common.h:87
struct probe * active_thread_entry_probe
struct probe * active_memory_probe_madvise
int32_t OFFSET
Definition: common.h:65
int os_linux_list_for_each_struct(struct target *t, struct bsymbol *bsymbol, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
Definition: list.h:51
struct probe * active_memory_probe_mmap_pgoff
struct symbol * os_linux_get_task_struct_type_ptr(struct target *target)
struct bsymbol * module_free_symbol
struct probe * active_thread_exit_probe
struct symbol * task_struct_type_ptr
num_t os_linux_get_preempt_count(struct target *target)
int os_linux_get_task_pid(struct target *target, struct value *task)
unsigned int thread_struct_has_ds_es
struct bsymbol * thread_exit_f_symbol
GHashTable * mm_addr_to_mm_cache
unsigned int thread_struct_has_debugreg0
struct memrange * range
struct symbol * thread_struct_type
struct bsymbol * thread_entry_v_symbol
struct value * os_linux_load_current_thread_as_type(struct target *target, struct symbol *datatype, REGVAL kernel_esp)
char * buf
Definition: target_api.h:3258
int(* os_linux_list_iterator_t)(struct target *t, struct value *value, void *data)
unsigned int thread_struct_has_perf_debugreg
struct probe * active_memory_probe
struct probe * active_memory_probe_mmap
const char * task_gid_member_name
struct probe * active_memory_probe_uselib
struct os_linux_vma * vma_cache
Definition: probe.h:308
struct bsymbol * modules
struct probe * debug_probe
struct value * os_linux_load_current_task(struct target *target, REGVAL kernel_esp)
uint32_t REGVAL
Definition: common.h:66
struct addrspace * space
struct bsymbol * thread_exit_v_symbol
unsigned int task_struct_has_thread_info
uint32_t ADDR
Definition: common.h:64
unsigned int last_thread_count
const char * thread_sp_member_name
struct probe * active_memory_probe_munmap
unsigned int pt_regs_has_fs_gs
unsigned int task_struct_has_stack
struct bsymbol * module_free_mod_symbol
unsigned int pt_regs_has_ds_es
struct symbol * thread_info_type
struct os_linux_vma * next
uint64_t unum_t
Definition: common.h:88
int os_linux_list_for_each_entry(struct target *t, struct bsymbol *btype, struct bsymbol *list_head, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
struct bsymbol * init_task
struct probe * active_memory_probe_mprotect
struct symbol * os_linux_get_thread_info_type(struct target *target)
struct target * t
Definition: dumptarget.c:48
const char * thread_ip_member_name
struct value * os_linux_get_task(struct target *target, tid_t tid)
int os_linux_get_task_tid(struct target *target, struct value *task)