Bitdefender Hypervisor Memory Introspection
lixprocess.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _LIXPROCESS_H_
6 #define _LIXPROCESS_H_
7 
8 #include "introtypes.h"
9 #include "update_guests.h"
10 #include "lixagent.h"
11 #include "lixddefs.h"
12 
13 #define LIX_COMM_SIZE 16u
14 
15 #define LIX_PROCESSES_MAX_COUNT 65536
16 
17 typedef struct _LIX_CREDS LIX_CREDS;
18 
22 typedef struct _LIX_TASK_PATH
23 {
25 
26  char *Path;
27  char *Name;
28 
30 
31  size_t PathLength;
32  size_t NameLength;
33 
36 
37 
38 typedef struct _LIX_TASK_OBJECT
39 {
41 
43 
44  char Comm[LIX_COMM_SIZE];
45 
46  char *Interpreter;
47 
48  char *CmdLine;
49 
51 
54  char *ProcName;
55 
59 
61 
66 
68 
71 
74 
76 
77  //
78  // Introspection-specific fields
79  //
81 
82  struct
83  {
87  } Protection;
88 
91 
92  void *HookObject;
93 
95  DWORD Exec: 1;
102 
104 
106 
107  struct
108  {
112  } UserStack;
113 
114  struct
115  {
118  } Dpi;
119 
121 
122 
123 static __forceinline QWORD
125  _In_ const LIX_TASK_OBJECT *Process
126  )
134 {
135  UNREFERENCED_PARAMETER(Process);
136 
138 }
139 
140 static __forceinline BOOLEAN
142  _In_ const LIX_TASK_OBJECT *Process,
143  _In_ QWORD Flag
144  )
154 {
155  return (Process->Protection.Mask & PROC_OPT_BETA) != 0 ||
157  (Process->Protection.Beta & Flag) != 0;
158 }
159 
160 static __forceinline BOOLEAN
162  _In_ const LIX_TASK_OBJECT *Process,
163  _In_ QWORD Flag
164  )
174 {
175  return ((Process->Protection.Feedback & Flag) ||
177 }
178 
182 typedef INTSTATUS
184  _In_ LIX_TASK_OBJECT *Task
185  );
186 
187 INTSTATUS
189  _In_ LIX_TASK_OBJECT *Task,
190  _Out_opt_ QWORD *StackPointer,
191  _Out_opt_ QWORD *StackBase,
192  _Out_opt_ QWORD *StackLimit
193  );
194 
195 INTSTATUS
197  _Out_ QWORD *InitTask
198  );
199 
202  _In_ DWORD CpuNumber
203  );
204 
205 INTSTATUS
207  _In_ DWORD CpuNumber,
208  _Out_ QWORD *TaskStruct
209  );
210 
211 QWORD
213  _In_ QWORD Cr3
214  );
215 
218  _In_ QWORD Cr3
219  );
220 
223  _In_ QWORD TaskStruct
224  );
225 
228  _In_ QWORD MmGva
229  );
230 
233  _In_ QWORD MmGva
234  );
235 
238  _In_ DWORD Pid
239  );
240 
241 INTSTATUS
243  _In_ const LIX_TASK_OBJECT *Task,
244  _Out_ LIX_TRAP_FRAME *TrapFrame
245  );
246 
247 DWORD
249  void
250  );
251 
252 INTSTATUS
254  _In_ void *Detour
255  );
256 
257 INTSTATUS
259  _In_ void *Detour
260  );
261 
262 INTSTATUS
264  _In_ void *Detour
265  );
266 
267 INTSTATUS
269  _In_ void *Detour
270  );
271 
272 INTSTATUS
274  _In_ void *Detour
275  );
276 
277 INTSTATUS
280  _In_ QWORD Aux
281  );
282 
283 INTSTATUS
285  _In_ QWORD TaskGva,
286  _In_ QWORD StaticDetected
287  );
288 
289 INTSTATUS
291  _In_ const char *ProcessName,
292  _In_ QWORD ProtectionMask,
293  _In_ QWORD Context
294  );
295 
296 INTSTATUS
298  _In_ const char *ProcessName
299  );
300 
301 void
303  void
304  );
305 
306 INTSTATUS
308  _Out_writes_bytes_(Length) char *CommandLine,
309  _In_ DWORD Length
310  );
311 
312 void
314  void
315  );
316 
317 void
319  void
320  );
321 
322 void
324  void
325  );
326 
327 void
329  void
330  );
331 
332 INTSTATUS
335  );
336 
337 void
339  _Out_ DWORD *Pids,
340  _In_ DWORD BufferSize
341  );
342 
343 BOOLEAN
345  void
346  );
347 
348 void
350  _In_ const void *Name,
351  _In_ const CAMI_STRING_ENCODING Encoding,
352  _In_ const CAMI_PROT_OPTIONS *Options
353  );
354 
355 INTSTATUS
357  _In_ void *Detour
358  );
359 
360 INTSTATUS
362  _In_ LIX_TASK_OBJECT *Task,
363  _In_ QWORD Ptr,
364  _Out_ BOOLEAN *IsPivoted
365  );
366 
367 #endif // _LIXPROCESS_H_
INTSTATUS IntLixTaskHandleDoExit(void *Detour)
Handles the exit() system call.
Definition: lixprocess.c:3481
BOOLEAN StolenTokens
TRUE if credentials for this process have been altered.
Definition: lixprocess.h:117
char * ProcName
The process name that is always valid. It's set depending which info is available in order: Path...
Definition: lixprocess.h:54
INTSTATUS IntLixTaskHandleFork(void *Detour)
Handles the fork() system call performed by a linux process.
Definition: lixprocess.c:3179
#define _Out_
Definition: intro_sal.h:22
_Bool BOOLEAN
Definition: intro_types.h:58
QWORD Cr3
The CR3 for this process.
Definition: lixprocess.h:70
static QWORD IntLixProcGetProtOption(const LIX_TASK_OBJECT *Process)
Returns the introcore options related to user mode protection.
Definition: lixprocess.h:124
LIX_TASK_OBJECT * IntLixTaskFindByCr3(QWORD Cr3)
Finds the Linux process having the provided Cr3.
Definition: lixprocess.c:942
SIZE_T ProcNameLength
The length of the ProcName field.
Definition: lixprocess.h:62
Describe the introcore protection options.
#define _In_
Definition: intro_sal.h:21
INTSTATUS IntLixTaskAdd(QWORD TaskGva, QWORD StaticDetected)
Creates and adds a Linux process in the internal list.
Definition: lixprocess.c:3996
DWORD MustKill
Will kill the process with the first occasion.
Definition: lixprocess.h:101
enum _LIX_AGENT_TAG LIX_AGENT_TAG
Tag used to identify an agent with a handler.
BOOLEAN IsPivoted
TRUE if this process stack is pivoted (used for DPI)
Definition: lixprocess.h:116
LIX_TASK_OBJECT * IntLixTaskProtFindByMm(QWORD MmGva)
Finds the protected Linux process having the provided mm guest virtual address.
Definition: lixprocess.c:974
CAMI_STRING_ENCODING
Describes the encoding of a string received from the CAMI file.
Definition: update_guests.h:52
#define _Out_writes_bytes_(expr)
Definition: intro_sal.h:38
QWORD RootProtectionMask
The protection that children will inherit.
Definition: lixprocess.h:89
QWORD Gva
The guest virtual address of the task_struct.
Definition: lixprocess.h:42
static BOOLEAN IntLixProcPolicyIsFeedback(const LIX_TASK_OBJECT *Process, QWORD Flag)
Verifies whether a specific process protection flag is in feedback only mode or not for a Linux proce...
Definition: lixprocess.h:161
QWORD Beta
The protection flags for this process that are in beta mode.
Definition: lixprocess.h:85
INTSTATUS IntLixTaskHandleVmRw(void *Detour)
Handles the process_vm_writev() system call.
Definition: lixprocess.c:3381
QWORD Parent
Depends if this is a thread or a process.
Definition: lixprocess.h:57
DWORD Tgid
The task Thread-Group-ID.
Definition: lixprocess.h:73
LIX_TASK_PATH * Path
The path of the file executed.
Definition: lixprocess.h:50
int INTSTATUS
The status data type.
Definition: introstatus.h:24
QWORD CreationTime
The creation timestamp for this process.
Definition: lixprocess.h:75
INTSTATUS IntLixTaskIsUserStackPivoted(LIX_TASK_OBJECT *Task, QWORD Ptr, BOOLEAN *IsPivoted)
Verifies whether the stack of a Linux process is pivoted or not.
Definition: lixprocess.c:2795
DWORD IsPreviousAgent
TRUE if this process is an agent remaining from a previous session.
Definition: lixprocess.h:98
QWORD Base
The user mode stack base.
Definition: lixprocess.h:109
DWORD RefCount
The number of references for this cache entry.
Definition: lixprocess.h:34
DWORD InterpLength
The length of the Interpreter field.
Definition: lixprocess.h:63
Describes a path cache entry.
Definition: lixprocess.h:22
BOOLEAN IntPolicyIsCoreOptionFeedback(QWORD Flag)
Checks if a core protection option is in feedback-only mode.
Definition: introcore.c:2829
QWORD RealParent
The process which called fork()
Definition: lixprocess.h:56
INTSTATUS(* PFUNC_LixTaskIterateTasks)(LIX_TASK_OBJECT *Task)
Definition: lixprocess.h:183
DWORD CommHash
The CRC32 checksum of the Comm field.
Definition: lixprocess.h:65
char * CmdLine
The process command line.
Definition: lixprocess.h:48
void IntLixTaskDumpProtected(void)
Dumps the list with processes that Introcore should protect.
Definition: lixprocess.c:4869
BOOLEAN IntPolicyCoreIsOptionBeta(QWORD Flag)
Checks if one of the kernel protection options is in log-only mode.
Definition: introcore.c:2603
QWORD ExeFileDentry
The guest virtual address of the executable file's "dentry" structure.
Definition: lixprocess.h:60
QWORD Limit
The user mode stack limit.
Definition: lixprocess.h:110
LIX_TASK_OBJECT * IntLixTaskFindByMm(QWORD MmGva)
Finds the Linux process having the provided mm guest virtual address.
Definition: lixprocess.c:999
void IntLixTaskDump(void)
Dumps the process list.
Definition: lixprocess.c:4797
INTSTATUS IntLixTaskIterateGuestTasks(PFUNC_IterateListCallback Callback, QWORD Aux)
Iterates the guest process list and calls the provided callback for each process and thread found...
Definition: lixprocess.c:3799
#define _Out_opt_
Definition: intro_sal.h:30
Describes one set of credentials.
Definition: lixcred.h:13
INTSTATUS IntLixTaskHandleExec(void *Detour)
Handles the exec() system call of a linux process.
Definition: lixprocess.c:2947
#define INTRO_OPT_PROT_UM_MISC_PROCS
Definition: intro_types.h:435
unsigned long long QWORD
Definition: intro_types.h:53
DWORD Exec
TRUE if the process did exec at least once.
Definition: lixprocess.h:95
struct _LIX_TASK_OBJECT LIX_TASK_OBJECT
QWORD IntLixGetKernelCr3(QWORD Cr3)
Transforms an user CR3 into a kernel CR3 on systems with KPTI enabled and active. ...
Definition: lixprocess.c:919
BOOLEAN Valid
TRUE if the values inside this structure are valid.
Definition: lixprocess.h:111
INTSTATUS IntLixTaskAddProtected(const char *ProcessName, QWORD ProtectionMask, QWORD Context)
Adds a protected process name pattern.
Definition: lixprocess.c:4334
DWORD IsThread
TRUE if it's a thread, not a process.
Definition: lixprocess.h:96
static BOOLEAN IntLixProcPolicyIsBeta(const LIX_TASK_OBJECT *Process, QWORD Flag)
Verifies whether a specific process protection flag is in beta mode or not for a Linux process...
Definition: lixprocess.h:141
INTSTATUS(* PFUNC_IterateListCallback)(QWORD Node, QWORD Aux)
Definition: introtypes.h:71
void IntLixTaskEnum(DWORD *Pids, DWORD BufferSize)
INTSTATUS IntLixAccessRemoteVmHandler(void *Detour)
Detour handler for __access_remote_vm.
Definition: lixprocess.c:5009
INTSTATUS IntLixTaskGetAgentsAsCli(char *CommandLine, DWORD Length)
Returns a string with the command lines of all active agents.
Definition: lixprocess.c:4525
DWORD ReExecToSelf
TRUE if the process is re-executed to self (exec to same executable).
Definition: lixprocess.h:100
QWORD Context
Context from integrator.
Definition: lixprocess.h:90
Exposes the definitions used by the CAMI parser and the functions used to load guest support informat...
LIST_ENTRY ExploitProtProcLink
Linkage in the protected processes list.
Definition: lixprocess.h:67
void IntLixProcUpdateProtectedProcess(const void *Name, const CAMI_STRING_ENCODING Encoding, const CAMI_PROT_OPTIONS *Options)
Updates the protection flags for Linux tasks that should be protected based on options received via C...
Definition: lixprocess.c:1173
INTSTATUS IntLixTaskRemoveProtected(const char *ProcessName)
Removes a pattern of processes to be protected.
Definition: lixprocess.c:4439
DWORD KernelMode
TRUE if this process/thread is inside kernel mode.
Definition: lixprocess.h:97
#define UNREFERENCED_PARAMETER(P)
Definition: introdefs.h:29
DWORD IntLixTaskGetExecCount(void)
Returns the number of processes that have performed an exec.
Definition: lixprocess.c:1077
char * Name
The path base name.
Definition: lixprocess.h:27
#define __forceinline
Definition: introtypes.h:61
uint32_t DWORD
Definition: intro_types.h:49
size_t NameLength
The size of the base name.
Definition: lixprocess.h:32
LIX_CREDS * Creds
The LIX_CREDS reference for the credentials of this process.
Definition: lixprocess.h:105
INTSTATUS IntLixTaskGetTrapFrame(const LIX_TASK_OBJECT *Task, LIX_TRAP_FRAME *TrapFrame)
Retrieves the trap frame for a Linux task.
Definition: lixprocess.c:1098
char * Interpreter
If this was a script executed through an interpretor.
Definition: lixprocess.h:46
QWORD DentryGva
The guest virtual address of the "dentry" structure associated with this path.
Definition: lixprocess.h:29
void IntLixTaskUpdateProtection(void)
Adjusts protection for all active Linux processes.
Definition: lixprocess.c:4495
INTSTATUS IntLixTaskGetUserStack(LIX_TASK_OBJECT *Task, QWORD *StackPointer, QWORD *StackBase, QWORD *StackLimit)
Finds the user mode stack limits for a Linux process.
Definition: lixprocess.c:2044
DWORD CmdLineLength
The length of the CmdLine field.
Definition: lixprocess.h:64
QWORD Feedback
The protection flags for this process that are in feedback-only mode.
Definition: lixprocess.h:86
void IntLixTaskUninit(void)
Uninitializes the Linux process subsystem.
Definition: lixprocess.c:4570
BOOLEAN IntLixTaskGuestTerminating(void)
Check whether the guest OS is terminating or not.
Definition: lixprocess.c:4923
size_t PathLength
The size of the path.
Definition: lixprocess.h:31
#define LIX_COMM_SIZE
The maximum size of the process comm.
Definition: lixprocess.h:13
void IntLixTaskDumpAsTree(void)
Dump the process tree.
Definition: lixprocess.c:4784
INTSTATUS IntLixTaskGetCurrentTaskStruct(DWORD CpuNumber, QWORD *TaskStruct)
Reads the guest virtual address of the task currently running on a CPU.
Definition: lixprocess.c:795
QWORD MmGva
The guest virtual address of the "mm_struct".
Definition: lixprocess.h:69
LIST_ENTRY Link
The list node.
Definition: lixprocess.h:24
INTSTATUS IntLixGetInitTask(QWORD *InitTask)
Finds the guest virtual address of the "init_task".
Definition: lixprocess.c:401
LIST_ENTRY Link
Linkage in the global task list.
Definition: lixprocess.h:40
struct _LIX_TASK_PATH LIX_TASK_PATH
Describes a path cache entry.
DWORD Pid
The task PID.
Definition: lixprocess.h:72
#define PROC_OPT_BETA
Process is monitored, but in log-only mode so no actions will be blocked.
Definition: intro_types.h:376
INTSTATUS IntLixTaskHandlePtrace(void *Detour)
Handles the ptrace() system call.
Definition: lixprocess.c:3423
DWORD Protected
TRUE if the process is protected.
Definition: lixprocess.h:99
LIX_TASK_OBJECT * IntLixTaskFindByGva(QWORD TaskStruct)
Finds Linux process with the provided "task_struct" guest virtual address.
Definition: lixprocess.c:1025
INTSTATUS IntLixTaskIterateTasks(PFUNC_LixTaskIterateTasks Callback)
Call the Callback parameter for each task saved internally.
Definition: lixprocess.c:4892
LIX_TASK_OBJECT * IntLixTaskGetCurrent(DWORD CpuNumber)
Finds the task that is currently running on the given CPU.
Definition: lixprocess.c:858
char * Path
The full path string.
Definition: lixprocess.h:26
void * HookObject
The HookObject used for EPT hooks set inside this process's memory space.
Definition: lixprocess.h:92
QWORD ActualParent
The parent, based on tgid. Only relevant for threads.
Definition: lixprocess.h:58
LIX_TASK_OBJECT * IntLixTaskFindByPid(DWORD Pid)
Finds the Linux process having the provided PID.
Definition: lixprocess.c:1051
LIST_HEAD Vmas
The list head for the VMAs from the memory space of this process.
Definition: lixprocess.h:80
DWORD StaticDetected
TRUE if the process was detected using a static scan (during static init).
Definition: lixprocess.h:94
size_t SIZE_T
Definition: intro_types.h:60
QWORD Mask
The protection flags enabled for this process.
Definition: lixprocess.h:84
LIX_AGENT_TAG AgentTag
The agent tag, if this process is an agent.
Definition: lixprocess.h:103