[PATCH v6][makedumpfile 08/10] Add erase_sample extension as an data erase reference

Tao Liu <[email protected]>
Newsgroups org.infradead.lists.kexec
Message-ID <[email protected]>
This patch will add a erase_sample.c as a data erase reference. It will
erase all tasks & threads's comm[] member within their task_struct, as
a result, you will see the ps info outputted by crash utility similiar
as follows:

crash> ps
 ...
 1       0  10  ffff96b7c0980000  IN   0.0    25504    15704  XXXXXXXXXXXXXXX
 2       0  15  ffff96b7c0982f40  IN   0.0        0        0  [XXXXXXXXXXXXXXX]
 3       2   2  ffff96b7c09a0000  IN   0.0        0        0  [XXXXXXXXXXXXXXX]
 4       2   0  ffff96b7c09a2f40  ID   0.0        0        0  [XXXXXXXXXXXXXXX]
 5       2   0  ffff96b7c09a8000  ID   0.0        0        0  [XXXXXXXXXXXXXXX]

The comm[] erasure doesn't cause critical info loss and has slight
impact on vmcore analyze, so it is safe to take it as an example for
developers.

Signed-off-by: Tao Liu <[email protected]>
---
 extensions/Makefile       |  2 +-
 extensions/erase_sample.c | 78 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 extensions/erase_sample.c

diff --git a/extensions/Makefile b/extensions/Makefile
index dbaec4e..c112d56 100644
--- a/extensions/Makefile
+++ b/extensions/Makefile
@@ -1,5 +1,5 @@
 CC ?= gcc
-CONTRIB_SO := sample.so
+CONTRIB_SO := sample.so erase_sample.so
 
 all: $(CONTRIB_SO)
 
diff --git a/extensions/erase_sample.c b/extensions/erase_sample.c
new file mode 100644
index 0000000..7c935d1
--- /dev/null
+++ b/extensions/erase_sample.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "../makedumpfile.h"
+#include "../btf_info.h"
+#include "../kallsyms.h"
+#include "../extension.h"
+#include "../erase_info.h"
+
+INIT_MOD_STRUCT_MEMBER(vmlinux, task_struct, tasks);
+INIT_MOD_STRUCT_MEMBER(vmlinux, task_struct, thread_node);
+INIT_MOD_STRUCT_MEMBER(vmlinux, task_struct, signal);
+INIT_MOD_STRUCT_MEMBER(vmlinux, signal_struct, thread_head);
+INIT_MOD_STRUCT_MEMBER(vmlinux, task_struct, comm);
+INIT_MOD_SYM(vmlinux, init_task);
+
+#define MOD_MEMBER_OFF(MOD, S, M) \
+	GET_MOD_STRUCT_MEMBER_MOFF(MOD, S, M) / 8
+#define KERN_MEMBER_OFF(S, M) MOD_MEMBER_OFF(vmlinux, S, M)
+#define GET_KERN_STRUCT_MEMBER_MSIZE(S, M) \
+	GET_MOD_STRUCT_MEMBER_MSIZE(vmlinux, S, M)
+#define GET_KERN_SYM(SYM) GET_MOD_SYM(vmlinux, SYM)
+
+/* task_struct.comm eraser */
+static bool erase_task_struct_comm(void)
+{
+	uint64_t task_list, init_task, comm, signal, thread_head, thread_list;
+	uint32_t size;
+
+	init_task = GET_KERN_SYM(init_task);
+	size = GET_KERN_STRUCT_MEMBER_MSIZE(task_struct, comm);
+	task_list = init_task + KERN_MEMBER_OFF(task_struct, tasks);
+
+	/* Iterate all tasks */
+	do {
+		thread_list = task_list - KERN_MEMBER_OFF(task_struct, tasks) +
+			KERN_MEMBER_OFF(task_struct, thread_node);
+		if (!readmem(VADDR, task_list - KERN_MEMBER_OFF(task_struct, tasks) +
+			KERN_MEMBER_OFF(task_struct, signal), &signal, sizeof(uint64_t))) {
+			ERRMSG("Can't get task_struct member signal!\n");
+			goto out;
+		}
+		thread_head = signal + KERN_MEMBER_OFF(signal_struct, thread_head);
+
+		/* Iterate all threads of the task */
+		do {
+			comm = thread_list - KERN_MEMBER_OFF(task_struct, thread_node) +
+				KERN_MEMBER_OFF(task_struct, comm);
+
+			if (!update_filter_info_raw(comm, 'X', size)) {
+				ERRMSG("Failed update filter info!\n");
+				goto out;
+			}
+
+			thread_list = next_list(thread_list);
+		} while (thread_list != thread_head);
+
+		task_list = next_list(task_list);
+	} while (task_list != init_task + KERN_MEMBER_OFF(task_struct, tasks));
+
+	return true;
+out:
+	return false;
+}
+
+void extension_init(void)
+{
+	if (!erase_task_struct_comm()) {
+		ERRMSG("erase_sample.so: erase fail!\n");
+		goto out;
+	}
+	MSG("erase_sample.so: erase success!\n");
+out:
+	return;
+}
+
+__attribute__((destructor))
+void extension_cleanup(void) { }
+
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.