Reposting this patch with a proper commit message following Greg's comment. No functional changes.
This patch adds the Tasklist sample module for Bootpatch-SLR. Building it requires setting CONFIG_SAMPLE_SPSLR_TASKLIST and CONFIG_SAMPLES and CONFIG_SAMPLES_SPSLR. The module provides a simple end-to-end demonstration of Bootpatch-SLR. When loaded, it prints the apparent offsets of several task_struct fields using ordinary offsetof() expressions. Pinpoint turns the underlying constants into instruction pins, allowing Selfpatch to rewrite them to the offsets in the randomized layout. The printed values therefore reflect the randomized layout selected at boot. The module then walks the task list using the randomized offsets, demonstrating that both the kernel and the module have been patched consistently. Signed-off-by: York Jasper Niebuhr <[email protected]> --- samples/Kconfig | 3 ++ samples/Makefile | 1 + samples/spslr/Kconfig | 17 ++++++++ samples/spslr/Makefile | 1 + samples/spslr/tasklist/Makefile | 1 + samples/spslr/tasklist/tasklist.c | 67 +++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 samples/spslr/Kconfig create mode 100644 samples/spslr/Makefile create mode 100644 samples/spslr/tasklist/Makefile create mode 100644 samples/spslr/tasklist/tasklist.c diff --git a/samples/Kconfig b/samples/Kconfig index a75e8e78330d..8925820617c9 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -326,6 +326,8 @@ source "samples/rust/Kconfig" source "samples/damon/Kconfig" +source "samples/spslr/Kconfig" + endif # SAMPLES config HAVE_SAMPLE_FTRACE_DIRECT @@ -333,3 +335,4 @@ config HAVE_SAMPLE_FTRACE_DIRECT config HAVE_SAMPLE_FTRACE_DIRECT_MULTI bool + diff --git a/samples/Makefile b/samples/Makefile index 07641e177bd8..1b727ceb2efa 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -45,3 +45,4 @@ obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/ obj-$(CONFIG_SAMPLE_DAMON_MTIER) += damon/ obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task/ obj-$(CONFIG_SAMPLE_TSM_MR) += tsm-mr/ +obj-$(CONFIG_SAMPLES_SPSLR) += spslr/ diff --git a/samples/spslr/Kconfig b/samples/spslr/Kconfig new file mode 100644 index 000000000000..ba163ea15006 --- /dev/null +++ b/samples/spslr/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 + +menuconfig SAMPLES_SPSLR + bool "SPSLR samples" + depends on SPSLR + help + You can build sample SPSLR kernel code here. + + If unsure, say N. + +if SAMPLES_SPSLR + +config SAMPLE_SPSLR_TASKLIST + tristate "SPSLR tasklist module example" + depends on m + +endif # SAMPLES_SPSLR diff --git a/samples/spslr/Makefile b/samples/spslr/Makefile new file mode 100644 index 000000000000..2d330b105c95 --- /dev/null +++ b/samples/spslr/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_SAMPLE_SPSLR_TASKLIST) += tasklist/ diff --git a/samples/spslr/tasklist/Makefile b/samples/spslr/tasklist/Makefile new file mode 100644 index 000000000000..6a5bc5b29b34 --- /dev/null +++ b/samples/spslr/tasklist/Makefile @@ -0,0 +1 @@ +obj-m += tasklist.o diff --git a/samples/spslr/tasklist/tasklist.c b/samples/spslr/tasklist/tasklist.c new file mode 100644 index 000000000000..ea021742c8e9 --- /dev/null +++ b/samples/spslr/tasklist/tasklist.c @@ -0,0 +1,67 @@ +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/sched/signal.h> +#include <linux/sched/task.h> +#include <linux/cred.h> +#include <linux/uidgid.h> +#include <linux/module.h> +#include <linux/list.h> + +static const struct task_struct module_target_data = { .flags = 42 }; + +static int __init taskinfo_init(void) +{ + struct task_struct *p; + + pr_info("taskinfo: loaded\n"); + pr_info(" offsetof(task_struct, pid)=%zu\n", offsetof(struct task_struct, pid)); + pr_info(" offsetof(task_struct, tgid)=%zu\n", offsetof(struct task_struct, tgid)); + pr_info(" offsetof(task_struct, cred)=%zu\n", offsetof(struct task_struct, cred)); + pr_info(" offsetof(task_struct, real_parent)=%zu\n", offsetof(struct task_struct, real_parent)); + pr_info(" offsetof(task_struct, comm)=%zu\n", offsetof(struct task_struct, comm)); + pr_info(" offsetof(task_struct, __state)=%zu\n", offsetof(struct task_struct, __state)); + pr_info(" offsetof(task_struct, flags)=%zu\n", offsetof(struct task_struct, flags)); + pr_info(" offsetof(task_struct, prio)=%zu\n", offsetof(struct task_struct, prio)); + pr_info(" offsetof(task_struct, policy)=%zu\n", offsetof(struct task_struct, policy)); + + pr_info("datapin flags value is %u (should be 42)\n", module_target_data.flags); + + pr_info("=== Task List ===\n"); + + rcu_read_lock(); + + for_each_process(p) { + const struct cred *cred; + struct task_struct *parent; + + cred = rcu_dereference(p->cred); + parent = rcu_dereference(p->real_parent); + + pr_info("task: pid=%d tgid=%d ppid=%d uid=%u gid=%u comm=%s state=%u flags=0x%x prio=%d policy=%u\n", + p->pid, + p->tgid, + parent ? parent->tgid : -1, + __kuid_val(cred->uid), + __kgid_val(cred->gid), + p->comm, + READ_ONCE(p->__state), + p->flags, + p->prio, + p->policy); + } + + rcu_read_unlock(); + + return 0; +} + +static void __exit taskinfo_exit(void) +{ + pr_info("taskinfo: unloaded\n"); +} + +module_init(taskinfo_init); +module_exit(taskinfo_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("List modules and tasks (task_struct stress)"); -- 2.43.0

