This commit is contained in:
zy
2023-11-16 17:39:26 +08:00
parent bd277b53c5
commit 67b53ae048
11 changed files with 710 additions and 639 deletions

View File

@@ -1,11 +1,12 @@
#include <linux/cdev.h> // for cdev
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/slab.h> // for kmalloc
#include "monitor_kernel_lib.c"
#include "monitor_kernel_task.c"
#include "monitor_kernel.h"
#define DEVICE_NAME "variable_monitor"
@@ -43,45 +44,16 @@ static int device_release(struct inode *inode, struct file *file) {
static long device_ioctl(struct file *file, unsigned int ioctl_num,
unsigned long ioctl_param) {
watch_arg warg;
void *kptr;
kernel_watch_timer *timer = NULL;
kernel_watch_arg k_watch_arg;
// copy watch_arg
if (copy_from_user(&warg, (watch_arg *)ioctl_param, sizeof(warg))) {
return -EACCES;
}
printk(KERN_INFO "Watch_arg: task_id=%d, name=%s, ptr=%p, length_byte=%d, "
"time_ns=%ld, threshold=%lld\n",
warg.task_id, warg.name, warg.ptr, warg.length_byte, warg.time_ns,
warg.threshold);
// user space address to kernel space address
kptr = convert_user_space_ptr(warg.task_id, (unsigned long)warg.ptr);
if (kptr == NULL) {
printk(KERN_ERR "Cannot access user space\n");
return -EACCES;
}
// check length
if (warg.length_byte != 1 && warg.length_byte != 2 && warg.length_byte != 4 &&
warg.length_byte != 8) {
printk(KERN_ERR "Invalid length %d\n", warg.length_byte);
return -EINVAL;
}
// k_watch_arg init
w_arg2k_w_arg(kptr, warg, &k_watch_arg);
timer = get_timer(warg.time_ns); // get a valuable timer
printk(KERN_INFO "ptr transform kptr: %p\n", kptr);
printk(KERN_INFO "timer: %p\n", timer);
printk(KERN_INFO "timer->sentinel: %d, timer->time_ns: %lld\n",
timer->sentinel, timer->time_ns);
printk(KERN_INFO "timer->hr_timer: %p\n", &timer->hr_timer);
TIMER_CANCEL(timer); // just in case
timer_add_watch(timer, k_watch_arg);
TIMER_START(timer);
printk(KERN_INFO "Start watching var: %s\n", warg.name);
// start watch variable
start_watch_variable(warg);
return 0;
}
@@ -131,6 +103,7 @@ int init_module(void) {
printk(KERN_INFO "dev number: %d\n", dev_num);
printk(KERN_INFO "path: /dev/%s %d\n", DEVICE_NAME, dev_num);
// orig_X | buffer
monitor_init();
return 0;
@@ -138,8 +111,9 @@ int init_module(void) {
void cleanup_module(void) {
printk(KERN_INFO "%s\n", __FUNCTION__);
// clear all timer and page list
clear_all_watch();
// clear all watch | free buffer
monitor_exit();
// unmount
device_destroy(watch_class, dev_num);
class_destroy(watch_class);
@@ -148,15 +122,3 @@ void cleanup_module(void) {
}
MODULE_LICENSE("GPL");
// one more thing
int monitor_init(void){
fn_kallsyms_lookup_name_init(); // init kallsyms_lookup_name
LOOKUP_SYMS(stack_trace_save_tsk); // stack_trace_save_tsk
LOOKUP_SYMS(show_stack); // show_stack
LOOKUP_SYMS(idle_sched_class); // idle_sched_class
LOOKUP_SYMS(access_remote_vm); // access_remote_vm
LOOKUP_SYMS_NORET(get_task_type); // get_task_type
LOOKUP_SYMS_NORET(kernfs_name); // kernfs_name
}