Bitdefender Hypervisor Memory Introspection
kthread.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "../common.h"
6 
7 typedef void * (kthread_create_on_node_fn)(int (*threadfn)(void *data), void *data, int node, const char namefmt[], ...);
8 typedef void * (vmalloc_exec_fn)(unsigned long size);
9 typedef int (wake_up_process_fn)(void *p);
10 typedef void *(__vmalloc_node_range_fn)(unsigned long size, unsigned long align, unsigned long start, unsigned long end,
11  unsigned int gfp_mask, unsigned long prot, unsigned long vm_flags, int node,
12  const void *caller);
13 
14 struct data {
16  struct {
17  unsigned long hypercall;
18  unsigned long completion;
19  unsigned long error;
20  } token;
21 
23  struct {
28  } func;
29 
31  struct {
32  unsigned long vmalloc_size;
33  } args;
34 };
35 
37 struct data _data __agent_data("kthread") = { 0 };
38 
39 __agent_text("kthread")
40 void kthread(void)
49 {
50  void *ptr = NULL;
51 
52  if (_data.func.vmalloc_exec)
53  {
54  ptr = _data.func.vmalloc_exec(_data.args.vmalloc_size);
55  }
56  else
57  {
58  ptr = _data.func.__vmalloc_node_range(_data.args.vmalloc_size, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL,
59  PAGE_KERNEL_EXEC, 0, -1, __func__);
60  }
61 
62  if (!ptr)
63  {
64  breakpoint_2(_data.token.error, _data.func.vmalloc_exec, 0);
65  return;
66  }
67 
68  void *entry_ptr = (void *)breakpoint_1(_data.token.hypercall, ptr);
69  void *task = _data.func.kthread_create_on_node(entry_ptr, NULL, -1, "bdagent");
70  if (IS_ERR_VALUE(task))
71  {
72  breakpoint_2(_data.token.error, _data.func.kthread_create_on_node, task);
73  return;
74  }
75 
76  int ret = _data.func.wake_up_process(task);
77  if (!ret)
78  {
79  breakpoint_2(_data.token.error, _data.func.wake_up_process, ret);
80  }
81 
82  breakpoint(_data.token.completion);
83 }
84 
85 
87 void trampoline(void)
95 {
96  kthread();
97 
98  __agent_exit("kthread");
99 }
struct data::@2 args
The arguments of the agent.
__vmalloc_node_range_fn * __vmalloc_node_range
Definition: kthread.c:27
#define VMALLOC_END
Definition: common.h:56
struct data::@0 token
The tokens used to communicate with Intocore.
unsigned long hypercall
Definition: kthread.c:17
static __default_fn_attr unsigned long breakpoint(unsigned long token)
Generate INT3 instruction for hypercall.
Definition: common.h:167
void *() kthread_create_on_node_fn(int(*threadfn)(void *data), void *data, int node, const char namefmt[],...)
Definition: kthread.c:7
#define GFP_KERNEL
Definition: common.h:15
#define IS_ERR_VALUE(x)
Definition: common.h:66
__agent_trampoline("kthread")
The trampoline of the agent.
Definition: kthread.c:86
vmalloc_exec_fn * vmalloc_exec
Definition: kthread.c:26
#define breakpoint_1(token, p1)
Hypercall using 1 argument.
Definition: common.h:177
struct data _data __agent_data("kthread")
The section used for this agent is .kthread_data'.
#define breakpoint_2(token, p1, p2)
Hypercall using 2 argument.
Definition: common.h:185
void *() __vmalloc_node_range_fn(unsigned long size, unsigned long align, unsigned long start, unsigned long end, unsigned int gfp_mask, unsigned long prot, unsigned long vm_flags, int node, const void *caller)
Definition: kthread.c:10
wake_up_process_fn * wake_up_process
Definition: kthread.c:25
#define PAGE_KERNEL_EXEC
Definition: common.h:58
#define __agent_exit(x)
Generates the exit asm-code using a label.
Definition: common.h:106
kthread_create_on_node_fn * kthread_create_on_node
Definition: kthread.c:24
int() wake_up_process_fn(void *p)
Definition: kthread.c:9
struct data::@1 func
The functions used by this agent.
unsigned long vmalloc_size
The size of allocation.
Definition: kthread.c:32
void *() vmalloc_exec_fn(unsigned long size)
Definition: kthread.c:8
unsigned long completion
Definition: kthread.c:18
unsigned long error
Definition: kthread.c:19
__fn_naked void trampoline(void)
The trampoline of the agent.
Definition: deploy.c:171
Definition: kthread.c:14
__agent_text("kthread")
Allocates a memory region with size of _data.args.vmalloc_size, deploy the main agent in that memory ...
Definition: kthread.c:39
#define VMALLOC_START
Definition: common.h:52