selfpatch-slr/subject/main.c

51 lines
1.5 KiB
C

#include <spslr.h>
#include "task_struct.h"
int second_pid();
int third_pid();
struct task_struct global = { .pid = 42, .comm = "main_global" };
static void print_layout() {
// TODO -> Make builtin __spslr_initial_offsetof(type, field) that is not patched
printf("Current task_struct layout:\n");
printf(" pid (int) : %2llu -> %2llu\n", 0, offsetof(struct task_struct, pid));
printf(" comm (const char*) : %2llu -> %2llu\n", 8, offsetof(struct task_struct, comm));
printf(" tasks (struct list_head) : %2llu -> %2llu\n", 16, offsetof(struct task_struct, tasks));
}
int main(void)
{
spslr_selfpatch();
print_layout();
struct list_head task_list;
INIT_LIST_HEAD(&task_list);
struct task_struct t1 = { .pid = 1, .comm = "init" };
struct task_struct t2 = { .pid = 2, .comm = "kthreadd" };
struct task_struct t3 = { .pid = 3, .comm = "worker" };
INIT_LIST_HEAD(&t1.tasks);
INIT_LIST_HEAD(&t2.tasks);
INIT_LIST_HEAD(&t3.tasks);
list_add_tail(&t1.tasks, &task_list);
list_add_tail(&t2.tasks, &task_list);
list_add_tail(&t3.tasks, &task_list);
printf("Task list:\n");
struct list_head *pos;
list_for_each(pos, &task_list) {
struct task_struct *task = list_entry(pos, struct task_struct, tasks);
printf(" pid=%d, comm=%s\n", task->pid, task->comm);
}
size_t myOffset = ((size_t)&((struct task_struct*)0)->tasks); // BROKEN, relevancy for kernel unknown
return second_pid() * third_pid();
}