114 lines
3.9 KiB
Diff
114 lines
3.9 KiB
Diff
From 478e0745ff6e37c03aa98f0883a18f7749d0afb2 Mon Sep 17 00:00:00 2001
|
|
From: jinlun <jinlun@huawei.com>
|
|
Date: Mon, 17 Jun 2024 19:28:54 +0800
|
|
Subject: [PATCH 04/14] fix trampoline
|
|
|
|
---
|
|
src/common/dim_baseline.h | 2 ++
|
|
.../dim_core_measure_process_elf.c | 33 +++++++++++++++++--
|
|
2 files changed, 32 insertions(+), 3 deletions(-)
|
|
|
|
diff --git a/src/common/dim_baseline.h b/src/common/dim_baseline.h
|
|
index 345b348..4292259 100644
|
|
--- a/src/common/dim_baseline.h
|
|
+++ b/src/common/dim_baseline.h
|
|
@@ -15,6 +15,7 @@ enum dim_baseline_type {
|
|
DIM_BASELINE_USER, /* baseline of user process */
|
|
DIM_BASELINE_KERNEL, /* baseline of kernel or kernel modules */
|
|
DIM_BASELINE_DATA,
|
|
+ DIM_BASELINE_TRAMPOLINE,
|
|
DIM_BASELINE_LAST,
|
|
};
|
|
|
|
@@ -22,6 +23,7 @@ static const char *const dim_baseline_name[DIM_BASELINE_LAST] = {
|
|
[DIM_BASELINE_USER] = "USER",
|
|
[DIM_BASELINE_KERNEL] = "KERNEL",
|
|
[DIM_BASELINE_DATA] = "DATA",
|
|
+ [DIM_BASELINE_TRAMPOLINE] = "TRAMPOLINE",
|
|
};
|
|
|
|
struct dim_baseline_tree {
|
|
diff --git a/src/core/tasks/dim_core_measure_process/dim_core_measure_process_elf.c b/src/core/tasks/dim_core_measure_process/dim_core_measure_process_elf.c
|
|
index 12040e2..df8d773 100644
|
|
--- a/src/core/tasks/dim_core_measure_process/dim_core_measure_process_elf.c
|
|
+++ b/src/core/tasks/dim_core_measure_process/dim_core_measure_process_elf.c
|
|
@@ -222,7 +222,10 @@ static int get_elf_measure_area(struct file *elf_file,
|
|
return ret;
|
|
}
|
|
|
|
- // TODO
|
|
+ /* check if it is no need to measure trampoline */
|
|
+ if (shdr_trampoline == NULL)
|
|
+ return 0;
|
|
+
|
|
ret = get_elf_section(elf_file, &ehdr, TRAMPOLINE_SECTION_NAME, shdr_trampoline);
|
|
if (ret == 0)
|
|
*shdr_trampoline_find = true;
|
|
@@ -256,6 +259,14 @@ static int measure_elf_trampoline(struct vm_area_struct *vma,
|
|
return ret;
|
|
}
|
|
|
|
+ /* for baseline mode, add an extra dynamic baseline of trampoline */
|
|
+ if (ctx->mode == DIM_BASELINE) {
|
|
+ ret = dim_measure_dynamic_baseline_add(ctx->m, ctx->path,
|
|
+ DIM_BASELINE_TRAMPOLINE, &digest);
|
|
+ if (ret < 0)
|
|
+ pr_warn("failed to add trampoline dynamic baseline\n");
|
|
+ }
|
|
+
|
|
return ctx->check(&digest, ctx);
|
|
}
|
|
|
|
@@ -295,6 +306,18 @@ static int measure_elf_text(struct vm_area_struct *vma,
|
|
return ctx->check(&digest, ctx);
|
|
}
|
|
|
|
+static bool trampoline_baseline_exist(struct task_measure_ctx *ctx)
|
|
+{
|
|
+ struct dim_digest digest = { 0 };
|
|
+
|
|
+ /* measure trampoline only the baseline is set */
|
|
+ return ctx->mode == DIM_BASELINE ?
|
|
+ (dim_measure_static_baseline_search(ctx->m, ctx->path,
|
|
+ DIM_BASELINE_TRAMPOLINE, &digest) == 0) :
|
|
+ (dim_measure_dynamic_baseline_search(ctx->m, ctx->path,
|
|
+ DIM_BASELINE_TRAMPOLINE, &digest) == 0);
|
|
+}
|
|
+
|
|
int measure_process_module_text_elf(struct vm_area_struct *vma,
|
|
struct task_measure_ctx *ctx)
|
|
{
|
|
@@ -304,6 +327,7 @@ int measure_process_module_text_elf(struct vm_area_struct *vma,
|
|
unsigned int phdrs_text_num = 0;
|
|
struct elf_shdr shdr_trampoline = { 0 };
|
|
bool shdr_trampoline_find = false;
|
|
+ bool trampoline_baseline_existed = false;
|
|
|
|
if (vma == NULL || !vma_is_file_text(vma) || ctx == NULL
|
|
|| ctx->m == NULL || ctx->check == NULL)
|
|
@@ -314,8 +338,11 @@ int measure_process_module_text_elf(struct vm_area_struct *vma,
|
|
return -ENOEXEC;
|
|
}
|
|
|
|
+ trampoline_baseline_existed = trampoline_baseline_exist(ctx);
|
|
+
|
|
ret = get_elf_measure_area(elf_file, &phdrs_text, &phdrs_text_num,
|
|
- &shdr_trampoline, &shdr_trampoline_find);
|
|
+ trampoline_baseline_existed ? &shdr_trampoline : NULL,
|
|
+ &shdr_trampoline_find);
|
|
if (ret < 0) {
|
|
dim_err("failed to get elf measure area from vma\n");
|
|
return ret;
|
|
@@ -331,7 +358,7 @@ int measure_process_module_text_elf(struct vm_area_struct *vma,
|
|
return ret;
|
|
}
|
|
|
|
- if (shdr_trampoline_find) {
|
|
+ if (shdr_trampoline_find && trampoline_baseline_existed) {
|
|
ret = measure_elf_trampoline(vma, base, &shdr_trampoline, ctx);
|
|
if (ret < 0) {
|
|
dim_err("failed to measure elf trampoline: %d\n", ret);
|
|
--
|
|
2.33.0
|
|
|